簡體   English   中英

Objective-C內存在addSubview上泄漏

[英]Objective-C Memory Leak on addSubview

泄漏儀器警告我與這部分代碼相關的內存泄漏:

[self.contview addSubview:nav.view];

以下是我管理視圖的方式:

    [nav.view removeFromSuperview];
    self.nav = [[[destinationClass alloc] initWithNibName:pagename bundle:nil] autorelease];
   [self.contview addSubview:nav.view];

self.nav剛剛分配后的retainCount是2是否正常?這可能與內存泄漏有關嗎?

我對記憶管理很新,有人可以給我一些幫助嗎?

非常感謝

假設nav是一個強(保留)屬性,它會保留您在此處指定的視圖控制器:

self.nav = [[[destinationClass alloc] initWithNibName:pagename bundle:nil] autorelease];

實際上,這行代碼后的保留計數為1; 對於allocretain +2,對於autorelease retain -1。 通常你永遠不應該使用retainCount方法來確定對象的實際保留計數,也許這個答案會讓你更深入地了解原因。 每個allocretaincopy調用都應該與releaseautorelease調用匹配。 您應該在類的dealloc方法中添加匹配的版本調用

-(void) dealloc {
    [_nav release];
    _nav = nil;
    [super dealloc];
}

不要使用手動內存管理,使用ARC,它會讓你的生活更輕松:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM