iDevGames Forums
removeFromSuperview not calling dealloc of view - SOLVED - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: removeFromSuperview not calling dealloc of view - SOLVED (/thread-7961.html)



removeFromSuperview not calling dealloc of view - SOLVED - BeyondCloister - Aug 8, 2010 10:03 AM

I have the following code:
Code:
if(viewController.view != nil) {
        // remove view from window's subviews
        [viewController.view removeFromSuperview];
    }
    
    self.screenSize = window.screen.bounds.size;
    self.screenScale = window.screen.scale;
    
    viewController.view = [[state alloc]
                               initWithFrame:CGRectMake(0, 0, (self.screenSize.height * self.screenScale), (self.screenSize.height * self.screenScale))
                               andManager:self];        
    
    [window addSubview:viewController.view];

state is one of many UIView based classes

The code is leaking memory as the current view's dealloc is not being called.

Previously (iPad iOS 3.2) I had the following at the start
Code:
if(viewController.view != nil) {
        // remove view from window's subviews
        [viewController.view removeFromSuperview];
        [viewController.view release];
    }

and it worked with no problems.

However switching the app to a universal app was causing crashes on the release line.

Any suggestions?


RE: removeFromSuperview not calling dealloc of view - SethWillits - Aug 8, 2010 03:07 PM

Well it's not being deallocated because you need that release call to balance the alloc.

Totally guessing: the reason it's crashing on the release line is probably something such as triggering a notification which is poking some other deallocated object. I would say it's a bad idea to have a deallocated object pointer (the view) assigned to the view controller, so that might be some place to start. The crash log should be very helpful.

You definitely need to balance the alloc so removing the release wasn't the correct fix.


RE: removeFromSuperview not calling dealloc of view - BeyondCloister - Aug 11, 2010 10:34 AM

The reason I removed the alloc is that when I investigated further it was my understanding that removeFromSuperview releases it. Of course I may have misunderstood what is in the docs
---
If the receiver’s superview is not nil, this method releases the receiver. If you plan to reuse the view, be sure to retain it before calling this method and be sure to release it as appropriate when you are done with it or after adding it to another view hierarchy.
---
Running this in the Simulator, the only message in the log is

Code:
*** -[UIView release]: message sent to deallocated instance 0x5a6c5d0



RE: removeFromSuperview not calling dealloc of view - BeyondCloister - Aug 11, 2010 12:46 PM

I have now solved the problem.

The parent UIViewController was using a Nib file and this was causing a default UIView to be created and thus this is what was being removed and released and causing the crash.

I now implement the UIViewController's loadView method and this means there is no view set when my code runs and so does not try releasing something that does not exist.

I've no idea why it worked without loadView in the iPad only build but it did. No crashes occurred during development or testing and no crash reports have been logged from the version on the App Store.


RE: removeFromSuperview not calling dealloc of view - SethWillits - Aug 11, 2010 01:10 PM

(Aug 11, 2010 10:34 AM)BeyondCloister Wrote:  The reason I removed the alloc is that when I investigated further it was my understanding that removeFromSuperview releases it. Of course I may have misunderstood what is in the docs
---
If the receiver’s superview is not nil, this method releases the receiver. If you plan to reuse the view, be sure to retain it before calling this method and be sure to release it as appropriate when you are done with it or after adding it to another view hierarchy.
---


Correct. addSubview: retains it, and removeFromSuperview releases it. But you've still alloc'd it, so you need to balance that, which you can do immediately after addSubview. Doing it there instead of after removeFromSuperview would solve the crash as you describe. The actual bug is interesting. That would have been tricky to figure out where a view came out of nowhere.


RE: removeFromSuperview not calling dealloc of view - BeyondCloister - Aug 11, 2010 01:18 PM

Quote: That would have been tricky to figure out where a view came out of nowhere.

It was tricky. It took several Red Bull fuelled readings through the UIViewController docs and my source code to track it down.