Voloda2
Voloda2

Reputation: 21

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

What is wrong ?

Upvotes: 0

Views: 4307

Answers (2)

executor21
executor21

Reputation: 4588

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.

Upvotes: 1

DigitalBytes
DigitalBytes

Reputation: 149

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

Upvotes: 0

Related Questions