开发者

Release and pushViewController

I have a custom view controller that I push onto my navigation stack as follows:

myViewController *myVC = [[myViewController alloc] init];
[myVC generate:myData];
[self.navigationController pushViewController:my开发者_Go百科VC animated:YES];

the code runs fine, but when checking for memory leaks I get a warning that myVC is never released.

adding a release statement

[myVC release];

in line 4 compiles fine, but crashes in execution. How do I need to handle this?


My guess is that it has nothing to do with this section of code. The problem is that before, since you weren't releasing it here, it was never deallocated. Now that it is being deallocated when you pop the view controller from the navigation stack, it's calling the dealloc method of myViewController, and some string object is being over-released in there.

Presumably at some point some string variable is set inside your myViewController without retaining it or it is released without being set to null, then later you release it again.


Yes, you should release your view controller.

The error message you see results from a memory management problem inside your view controller - it would seem that you over-release a NSString object.


You should use

myViewController *myVC = [[myViewController alloc] initWithNibName:nil bundle:nil]

When creating new view controllers, it seems that using init instead of initWithNibName with nil arguments messes things up for pushViewController. This was a problem that took me hours to solve since it seems unrelated to the issue at hand!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜