2011-03-10 107 views
0

我很迷惑removeFromSuperview的內存管理。子視圖沒有調用dealloc當超級查看(UIViewController)dealloc

這裏是我的代碼:

MySubView *tMySubView = [[MySubView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
tMySubView.center = self.view.center; 
tMySubView.tag = 1111; 
[self.view addSubview:tMySubView]; 
[tMySubView release]; 

「自我」 是的UIViewController。

當「自我」調用dealloc但MySubView沒有調用dealloc。

我知道addSubView retainCount +1。

所以我嘗試添加[tMySubView removeFromSuperview]在 「自我」 的dealloc

而且MySubView的dealloc被稱爲...

我應該添加[子視圖removeFromSuperview]當superView dealloc?

或者superView removeFromSuperview,它會自動調用subView的removeFromSuperview ...?

我找不出來。 :(

謝謝!

回答

0

這意味着self.view不能正常釋放,這將刪除所有它的子視圖只有在情況下,當它被釋放。

檢查你的loadView方法(在那裏你大概初始化),如果它看起來像這樣:

-(void) loadView { 
    self.view = [[UIView alloc] init]; 
} 

那麼你有泄漏,必須重寫它是這樣的:

-(void) loadView { 
    self.view = [[[UIView alloc] init] autorelease]; 
} 

OR

-(void) loadView { 
    UIView* v = [[UIView alloc] init]; 
    self.view = v; 
    [v release]; 
} 

同時檢查所有在那裏你所訪問self.view的地方,並確保你沒有過保留它。

+0

MyViewController * tMyViewController = [[[MyViewController alloc] init] autorelease]; \t [tNavigationController pushViewController:tMyViewController animated:YES];我這樣寫。 – wei 2011-03-10 10:43:41

+0

不要混淆viewcontroller和它的視圖。 – Max 2011-03-10 10:49:32

+0

如何創建視圖?在loadView中還是在XIB中? – hennes 2011-03-10 10:53:20