views:

542

answers:

2

I have a very simple code to show a modal controller (nextController is a class member):

nextController = [[InstructionsScreen alloc] initWithNibName:@"InstructionsScreen" bundle:nil];
[self presentModalViewController:nextController animated:YES];
[nextController release];

And then when the controller should hide:

[self dismissModalViewControllerAnimated:YES];
nextController = nil;

All works good as expected, but when I run instrument Object Allocations it shows that after dismissing the modal controller the memory it allocated is not freed. This becomes a problem because when I show several controllers the memory is over ...

Can anybody give me some clues ? Clang doesn't see any problems, so I'm stuck hitting the memory limit, because the memory of the dismissed controllers won't get released.


EDIT: What I discovered up to now is that it seems to be a leak somewhere in Apple's stuff. Way to reproduce: XCode -> create new project with the template "Utility application". Don't write any code yourself. Just create a new utility application and run it with "Object allocations", choose to see "Created & Still living". Now flip the modal controller few times - you'll see the allocated memory only grows and grows every time the modal controller is appearing and when it's disappearing too ...

A: 

Modal views are not subviews of the calling view but are instead subview of the apps window and are retained by the window itself. You generally you do not retain a reference to them in the controller that calls them. Instead, evoke the modal view and then have it communicate with the controller by defining the controller as the modal view's delegate.

I think that if you use synthesize to create the accessor for a nextController property defined with retain, then the accessor will retain any object assigned to the property. Simply setting the value to nil will not release the object unless the accessor is set up to do that and I don't think the autogenerated ones do.

You will expressly have to call release before setting to nil.

If this doesn't work, post the code for your definition of the nextController property.

TechZen
As I said nextController is a member, not a property:@interface MainScreenViewController : UIViewController{ id nextController;}using "release" instead of assigning nextController to "nil" leads to an exception: message viewDidDisappear: sent to freed object.The Object seems to be freed, however "created and still alive" object allocation instrument, shows only growing and growing memory usage.
Ican Zilb
A: 

The typical idiom for displaying a modal view controller is to use a local variable to store the pointer until you display and release it.

Like TechZen said, if nextController is a property with the retain attribute set, assigning an object created with alloc-init (which in itself sets the retain count to one) to it will increment the retain count twice.

Frank Schmitt
As you can see in the code given above I am not accessing a property, but a class variable, thus there is no setter accessor "retain" which will overretain nextController
Ican Zilb