开发者

How to delete UIViewController corectly?

MyPlatesViewController* viewController = [[MyPlatesViewController alloc] initWithNibName:@"MyPlates" bundle:nil ];
[self.view addSubview:viewController.view];

then i delete my viewController

[self.view removeFromSuperview];

but leak instrument shows 20 MB memory

开发者_C百科

What is wrong ?


You leaked the view controller object. After you remove the view from its superview, you need to release the controller as well.

Alternatively, you can do the following:

[self presentModalViewController:viewController animated:NO];
[viewController release];

Then, when dismissModalViewController is called, both the view and the view controller will be released properly.


You called alloc so it's your responsibility to release it. Your code should look like this:

MyPlatesViewController* viewController = [[MyPlatesViewController alloc] initWithNibName:@"MyPlates" bundle:nil ];
[self.view addSubview:viewController.view];
[viewController release]

Note that your controller is retained by the view when you call addSubview and released when you call removeFromSuperview. So with your current code the retain count of viewController is still 1 after calling removeFromSuperview.

Additionally you should review objective-c memory manament here: http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜