开发者

Problem with dealloc method not being called in UIViewController

im having a problem where the dealloc method for my custom UIViewController (MyVC) is not being called when it is popped. Read a couple of other answers on SO and it seems one culprit might be that i am still holding a reference to MyVC somewhere which is why dealloc is not being called.

Obj *objA = [[Obj alloc] initWithValue:value];

MyVC *newVC = [[MyVC alloc] init];
newVC.somIvar = objA;

[self.navigationController pushViewController:newVC animated:YES];

[newVC release];
[objA release];

What i am still confused about is does this means that the root view controller (RootVC) that pushes MyVC still has a reference to it or that something inside MyVC itself is not being released which seems odd as its dealloc method isnt called so obv its still has references to all its objects?

As far as i understand it the order of events from pushing to popping MyVC are:

  1. create and push MyVC from RootVC as described above
  2. when the back button is pressed on the navBar, popViewControllerAnimated: is called in navigationController and MyVC is popped
  3. if there are no further references to the MyVC then navigationController calls its dealloc method?

Not sure about number 3? I have not found any leaks using inst开发者_C百科ruments and it seems that it cant be something inside MyVC because its dealloc method is not even called so how would it clean it anyway?


Since you're using the dot-notation to assign your vC to the iVar of the other object, it is being retained (assuming you've declared retain in the property declaration) by that object.

Unless you release the iVar (and hence vC) in the object'c class' dealloc method, it would not be released even if object pops. So, add [somIvar release] to objects dealloc method and all should be well...

Btw, you're safe to release vC right after assigning it to the iVar, since it will be retained by object until it's released itself.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜