Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-17-2010, 04:21 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 6
arsenikal is on a distinguished road
Default Problems releasing memory when popping a viewController using a navigationController

I'm having the following problem. When I pop the view controller pushing the back button, the dealloc method is not getting called.

Here is the code I'm using:

Code:
NSLog(@"coleccionVista retain count0: %i",[coleccionVista retainCount]);
    
    coleccionVista = [[coleccionViewController alloc] init];
    NSString *nombreColeccion = [colecciones objectAtIndex:i];
    coleccionVista.nombreColeccion = nombreColeccion;
    coleccionVista.title = [NSString stringWithFormat:coleccionVista.nombreColeccion];
    NSLog(@"coleccionVista retain count1: %i",[coleccionVista retainCount]);
    
    [self.navigationController pushViewController:coleccionVista animated:NO];
    NSLog(@"coleccionVista retain count2: %i",[coleccionVista retainCount]);
    
    [coleccionVista release];
    //[coleccionVista release];
    NSLog(@"coleccionVista retain count3: %i",[coleccionVista retainCount])
;

And I'm getting these messages on the console:

First time I push the view:

Code:
2010-08-17 10:30:36.019 TAU 4[50133:207] coleccionVista retain count0: 0
    2010-08-17 10:30:36.021 TAU 4[50133:207] coleccionVista retain count1: 1
    2010-08-17 10:30:36.022 TAU 4[50133:207] coleccionVista retain count2: 3
    2010-08-17 10:30:36.022 TAU 4[50133:207] coleccionVista retain count3: 2
    2010-08-17 10:30:36.088 TAU 4[50133:207] coleccionViewController->viewWillAppear
    2010-08-17 10:30:38.515 TAU 4[50133:207] coleccionViewController->viewWillDisappear
Second time:

Code:
2010-08-17 10:30:44.171 TAU 4[50133:207] coleccionVista retain count0: 1
    2010-08-17 10:30:44.173 TAU 4[50133:207] coleccionVista retain count1: 1
    2010-08-17 10:30:44.174 TAU 4[50133:207] coleccionVista retain count2: 3
    2010-08-17 10:30:44.176 TAU 4[50133:207] coleccionVista retain count3: 2
    2010-08-17 10:30:44.241 TAU 4[50133:207] coleccionViewController->viewWillAppear
    2010-08-17 10:30:52.332 TAU 4[50133:207] coleccionViewController->viewWillDisappear
I also have a NSLog message on the dealloc method that isn't showing. But I've noticed that if I force another [coleccionVista release] after the other one the dealloc message is showed but crashing when trying to [super dealloc]. I'm not holding any other reference of coleccionViewController (I've been searching in the code and all the uses of the method are in the code I'm showing to you).

Any idea? Thanks in advance!
arsenikal is offline   Reply With Quote
Old 08-17-2010, 05:00 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Calling release does not necessarily release the memory at that point. Instead it tells the OS that it CAN release the memory when ready.

So don't panic and re-read the iPhone Memory Guidelines again
wuf810 is offline   Reply With Quote
Old 08-17-2010, 05:27 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 6
arsenikal is on a distinguished road
Default

Quote:
Originally Posted by wuf810 View Post
Calling release does not necessarily release the memory at that point. Instead it tells the OS that it CAN release the memory when ready.

So don't panic and re-read the iPhone Memory Guidelines again
Yes, I see your point. But I have a NSLog message in the dealloc method and it never gets called, so I'm never releasing that memory.
arsenikal is offline   Reply With Quote
Old 08-17-2010, 05:31 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by arsenikal View Post
Yes, I see your point. But I have a NSLog message in the dealloc method and it never gets called, so I'm never releasing that memory.
Dealloc gets called when the class gets released. So this may not be happening because it is retained or the os doesn't need to release it.

This is normal. Leave the breakpoint in there and at some point it will get called.

As long as the dealloc method is releasing all the objects you need to release, then don't worry about it.
wuf810 is offline   Reply With Quote
Old 08-17-2010, 05:38 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 6
arsenikal is on a distinguished road
Default

Quote:
Originally Posted by wuf810 View Post
Dealloc gets called when the class gets released. So this may not be happening because it is retained or the os doesn't need to release it.

This is normal. Leave the breakpoint in there and at some point it will get called.

As long as the dealloc method is releasing all the objects you need to release, then don't worry about it.
The problem is that if I load the program into the device, after using the app some time, it gets a memory warning and crashes without deallocating. So it must be some other mistake. But thanks for your quick answer.
arsenikal is offline   Reply With Quote
Old 08-17-2010, 05:43 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by arsenikal View Post
The problem is that if I load the program into the device, after using the app some time, it gets a memory warning and crashes without deallocating. So it must be some other mistake. But thanks for your quick answer.
Ok that's slightly different. So in the memory warning delegate methods you want to try and release things like your xibs, any large images etc. Basically anything that can be recreated easily.
wuf810 is offline   Reply With Quote
Old 08-17-2010, 05:59 AM   #7 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 6
arsenikal is on a distinguished road
Default

Quote:
Originally Posted by wuf810 View Post
Ok that's slightly different. So in the memory warning delegate methods you want to try and release things like your xibs, any large images etc. Basically anything that can be recreated easily.
Yes, I know. But that's not the point. the viewController never gets deallocated and is the reason of the memory warning. Thanks anyway .Any other ideas?
arsenikal is offline   Reply With Quote
Old 08-17-2010, 06:58 AM   #8 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by arsenikal View Post
Yes, I know. But that's not the point. the viewController never gets deallocated and is the reason of the memory warning. Thanks anyway .Any other ideas?
So I don't think I explained myself very well. Maybe Apple can do a better job:

Quote:
When a view controller receives a memory warning (didReceiveMemoryWarning), it should relinquish ownership of resources that are currently not needed and that can be recreated later if required. One such resource is the view controller's view itself. If it does not have a superview, the view is disposed of (in its implementation of didReceiveMemoryWarning, UIViewController invokes [self setView:nil]).
So are you releasing and nilling the viewController itself? If not it will never get a dealloc.

Hope this helps. M.
wuf810 is offline   Reply With Quote
Old 08-17-2010, 09:10 AM   #9 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 6
arsenikal is on a distinguished road
Default

Quote:
Originally Posted by wuf810 View Post
So I don't think I explained myself very well. Maybe Apple can do a better job:



So are you releasing and nilling the viewController itself? If not it will never get a dealloc.

Hope this helps. M.
Your explanation was very clear. I understood you perfectly. And I release all the stuff that isn't necessary in the didReceiveMemoryWarnin. But I get the memory warning because it never gets deallocated, so each time I push again the viewController the program gets more and more used memory. After that viewController I push more views into the stack and when I pop one (pushing the back button), it gets deallocated as expected, but not with this one.
arsenikal is offline   Reply With Quote
Old 08-19-2010, 11:52 AM   #10 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 6
arsenikal is on a distinguished road
Default

Finnally I think I figured out what happened. I've changed a lot of code, so I'm not sure about it, but it seems that it was a NSTimer that was using a method of the class coleccionVista, so it was maintaining a reference of the class so it was impossible to deallocate it.
arsenikal is offline   Reply With Quote
Reply

Bookmarks

Tags
back button, iphone, pushviewcontroller, release, uinavigationcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 362
5 members and 357 guests
freewind, givensur, linkmx, Newbie123, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0