Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Memory Leak in Dismissing Modal View Controller

In controller named controller1, I am pushing a modal view controller
AddConversationViewController *addController = [[AddConversationViewController alloc] 
initWithNibName:@"AddConversationViewController" bundle:nil];
//addController.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];
[addController release];
[navigationController release];


nd then in that addcontroller, I have allocated several object. but in the dealloc method, when i release those objects, I will get BAD_ACCESS warning when i dismiss the modal view controller. If I don't release those objects I have allocated, it doesn't give the BAD_ACCESS warning.
those objects I have allocated before are not released nor retained.

Does anybody know how to fix this memory leak?

iPhone OS 3.1.3

Posted on Jun 22, 2010 6:12 AM

Reply
Question marked as Best reply

Posted on Jun 22, 2010 9:12 PM

Hi David -

I don't think you have a memory leak. It seems release is being sent to one or more invalid addresses in the dealloc method of the root view controller or its superclass.

Here are two common ways that could happen:

1) viewDidUnload wasn't implemented correctly. For example, a view object retained by a property may have been released without setting the property to nil;

2) A view object such as UIButton may have been obtained from a convenience method and added to the content view without being retained by a property setter. In that case, the object's retain count will go to zero when it's released in dealloc. But the object will then be released again when the content view is released in [super dealloc].

You can catch a problem like no. 2 by looking for statements like this:

myButton = // <-- direct assignment to instance variable
[UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:myButton];

Since myButton was obtained from a convenience method, it's autoreleased. It's then retained when added to the superview so at the end of the event cycle, myButton will have a retain count of 1. As explained above, this means its count will go to zero if it's released in dealloc, and the program will then crash in [super dealloc[.

So what's wrong with the example code? The problem is in the first line. When the address of a control is saved in a property, we normally want it to be retained so the total retain count will be 2 after the control is added to the superview. But myButton= doesn't retain because it's a direct assignment rather than a call to the property setter. The code should have been:

self.myButton = // <-- dot syntax invokes setter method
[UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:myButton];

Because of the dot syntax, the first statement is equivalent to:

[self setMyButton:[UIButton buttonWithType:UIButtonTypeRoundedRect]];

So 'self.myButton=' will increment the retain count while 'myButton=' will not.

Of course only a couple of the most likely mistakes are discussed here. At this point, the chances of guessing your exact problem are slim to none (and slim may have just left the building). But now you know what you're looking for, right?

If none of the above is helpful, we'll probably need to see most or all of the code for your AddConversationViewController class, and maybe a description of its xib file as well.

- Ray
2 replies
Question marked as Best reply

Jun 22, 2010 9:12 PM in response to Daviiidddd

Hi David -

I don't think you have a memory leak. It seems release is being sent to one or more invalid addresses in the dealloc method of the root view controller or its superclass.

Here are two common ways that could happen:

1) viewDidUnload wasn't implemented correctly. For example, a view object retained by a property may have been released without setting the property to nil;

2) A view object such as UIButton may have been obtained from a convenience method and added to the content view without being retained by a property setter. In that case, the object's retain count will go to zero when it's released in dealloc. But the object will then be released again when the content view is released in [super dealloc].

You can catch a problem like no. 2 by looking for statements like this:

myButton = // <-- direct assignment to instance variable
[UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:myButton];

Since myButton was obtained from a convenience method, it's autoreleased. It's then retained when added to the superview so at the end of the event cycle, myButton will have a retain count of 1. As explained above, this means its count will go to zero if it's released in dealloc, and the program will then crash in [super dealloc[.

So what's wrong with the example code? The problem is in the first line. When the address of a control is saved in a property, we normally want it to be retained so the total retain count will be 2 after the control is added to the superview. But myButton= doesn't retain because it's a direct assignment rather than a call to the property setter. The code should have been:

self.myButton = // <-- dot syntax invokes setter method
[UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:myButton];

Because of the dot syntax, the first statement is equivalent to:

[self setMyButton:[UIButton buttonWithType:UIButtonTypeRoundedRect]];

So 'self.myButton=' will increment the retain count while 'myButton=' will not.

Of course only a couple of the most likely mistakes are discussed here. At this point, the chances of guessing your exact problem are slim to none (and slim may have just left the building). But now you know what you're looking for, right?

If none of the above is helpful, we'll probably need to see most or all of the code for your AddConversationViewController class, and maybe a description of its xib file as well.

- Ray

Aug 17, 2010 7:24 AM in response to RayNewbie

hey ray, I have found the issue.
it is because I am calling
[addcontroller release];
after pushing the navigation controller.
what's gonna happen is that because after pushing the nav controller, addcontroller will be released. so all the objects in addcontroller will also be release.
this will cause the problem when addcontroller is dismissed because it will be released again.

Memory Leak in Dismissing Modal View Controller

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.