Marin Todorov
Marin Todorov

Reputation: 6397

Using dismissModalViewControllerAnimated won't free any memory

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 ...

Upvotes: 8

Views: 2817

Answers (2)

Stephen Darlington
Stephen Darlington

Reputation: 52565

There is no leak in the code you show as far as I can see. There could be a leak in InstructionsScreen that would prevent it being deallocated.

I think it's worth running the Static Analyser to see if it finds a leak.

The leak in the Apple template code is interesting. It could be that there is a leak. It seems unlikely but obviously it's not impossible. I would say that it's more likely that it's a false-positive in Instruments, which is why I'd suggest using the Static Analyser.

(You might want to raise a bug report about the leak.)

Upvotes: 1

TechZen
TechZen

Reputation: 64428

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.

Upvotes: 0

Related Questions