开发者

subview didn't call dealloc when superview(UIViewController) dealloc

I was confuse with memory management of removeFromSuperview.

Here is my code:

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

"self" is UIViewController.

When "self" call dealloc but MySubView didn't call dealloc.

I know addSubView retainCount +1.

So I try add [tMy开发者_如何学CSubView removeFromSuperview] in "self" dealloc

And MySubView dealloc was called...

Should I add [subView removeFromSuperview]; when superView dealloc?

Or superView removeFromSuperview ,it will automaticly call subView's removeFromSuperview...?

I can't figure it out. :(

Thanks!


It means that self.view is not properly released. It will remove all it's subviews only in case when it is deallocated.

Check your loadView method (where you're probably initialize it). If it looks like this:

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

then you've got leak and have to rewrite it like this:

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

OR

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

Also check all the places where you're accessing self.view and make sure you're not over-retaining it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜