2010-09-22 125 views
1

使用Apple的PhotoScroller示例重新使用分配給視圖的內存,一旦保留計數達到0,我就無法釋放內存。這裏是我的代碼,以便更好地理解: 這篇文章摘自PhotoScroller
PhotoViewController.m從UIScrollView中移除視圖

- (void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index 
{ 
    page.index = index; 
    page.frame = [self frameForPageAtIndex:index]; 
    [page displayPage:index]; 
} 

ImageScrollView.h

@interface ImageScrollView : UIView 
{ 

    UIViewController *vc; 
    NSUInteger  index; 

} 
@property (assign) NSUInteger index; 

- (void)displayPage:(int)indice; 

@end 

ImageScrollView.m

- (void)dealloc 
{  
[vc release]; 
    [super dealloc]; 
} 

- (void)displayPage:(int)indice 
{ 

    //remove previous view 
    [vc.view removeFromSuperview]; 
    [vc release]; 
    vc = nil; 
    //NSLog(@"vc retain %i", [vc retainCount]); 

    // make a new viewController for the new page 
    Class clss = NSClassFromString([NSString stringWithFormat:@"page_0%i", indice + 1]); 
    vc = [[clss alloc] initWithNibName:[NSString stringWithFormat:@"page_0%i", indice + 1] bundle:nil]; 

    [self addSubview:vc.view]; 
} 

的類 「page_0x」 是UIViewControllers。到目前爲止,XIB上只有一個UIImageView。 一個page_01的例子:
@implementation page_01

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Overriden to allow any orientation. 
    return YES; 
} 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 


- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 

存儲器峯到裝置148 MB。給我一條內存警告級別1,然後釋放一些內存。下降到95 MB。繼續上下。

回答

0

也許你的page_0x視圖控制器被釋放,但他們創建的東西不是。有些事情要檢查:

  1. 在page_0x視圖控制器的dealloc方法設置一個斷點。它會被叫到嗎?

  2. 檢查page_0x視圖控制器的所有IBOutlet和其他實例變量。他們是否都在他們的課程dealloc方法中正確釋放?

  3. 運行構建&分析。它會變成什麼?

  4. 嘗試運行泄漏儀器。它可以告訴你什麼樣的物體實際上泄漏。

  5. (編輯)現在抓住吸管。你沒有打開NSZombieEnabled,是嗎?

  6. (編輯2)你說你扔了一個.png。如果你刪除那個.png會怎麼樣?

  7. 如果您正在模擬器中運行,請在設備上嘗試。 (見this question。)

+0

1. dealloc正在被調用。 2.我的page_0x viewcontrollers上沒有任何內容。到目前爲止,我只是在向它扔一個png。 3.構建和分析不會給我任何奇怪的東西。 4.沒有泄漏。 – GianPac 2010-09-22 12:43:14

+0

否NSZombieEnabled。但我注意到,當我得到一個內存警告級別1時,一些內存被取消分配。它從148MB到95MB。然後它再次增加,直到它遇到另一個記憶警告。我現在的問題是,如果每次對象的保留計數爲0時我應該排空游泳池。 – GianPac 2010-09-22 15:13:10

+0

設備上沒有泄漏。如果我用png刪除UIImageView,似乎沒有任何改變。它似乎很尷尬,它等待內存警告實際上釋放內存 – GianPac 2010-09-22 17:09:49