views:

191

answers:

2

Hi,

I'm dealing with viewDidUnload and dealloc methods and I've founded a problem when calling [super dealloc]; in parent view controller.

I have a lot of view controllers with custom code which I have putted outside on a parent view controller. So, when defining my view controllers I set a reference to the super class:

@interface LoginViewController : AbstractViewController

Then, at the dealloc method I call the AbstractViewController dealloc method:

//(Login View Controller code)
- (void)dealloc {
    [user release];
    [passwd release];
    [super dealloc];
}

[super dealloc] execute the following code:

//(Abstract View Controller code)
- (void)dealloc {
    [dbUtils release];
    [loadingView release];
    [super dealloc];
}

If I simulate a memory warning on iPhone Simulator, the following exception is thrown:

2010-03-03 11:27:45.805 MyApp[71563:40b] Received simulated memory warning.
2010-03-03 11:27:45.808 MyApp[71563:40b] *** -[LoginViewController isViewLoaded]:     message sent to deallocated instance 0x13b51b0
kill
quit

However, if I comment the [super dealloc] line in AbstractViewController the exception is not thrown and my app still running.

Thank you for your help once again!

A: 

It looks like you're freeing your view controller but then trying to use it again. When you free your view controller because of the memory warning, remember to set the pointer to nil so you don't accidentally use it again. i.e. something like

[myLoginViewController release]; //!< Triggers the dealloc call
myLoginController = nil; //!< Makes sure we never use it again

or, if myLoginViewController is a property you can do this in a neater way :

self.myLoginViewController = nil;

Hope that helps,

Sam

deanWombourne
A: 

Notice -didReceiveMemoryWarning does not trigger -dealloc, it triggers -viewDidUnload. So I guess it is your implementation of -viewDidUnload that does something wrong that causes the controller's final retention to be released so -dealloc is called. I've just encountered this problem in my code which is caused by a retain recycle that is released in -viewDidUnload.

an0