views:

22

answers:

2

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:

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:

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:

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!

A: 

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.

A: 

Two big problems:

NSLog(@"coleccionVista retain count0: %i",[coleccionVista retainCount]);
coleccionVista = [[coleccionViewController alloc] init];

Presumably coleccionVista might be non-nil, but you are not releasing it before assgning a new one. This is either a leak or a crash. Also note that -retainCount returns NSUInteger, not int; formatting it with %i invokes undefined behaviour (normally this is just printing the wrong number on big-endian 64-bit systems).

[coleccionVista release];
NSLog(@"coleccionVista retain count3: %i",[coleccionVista retainCount]);

You're releasing something. You no longer own it. It is not safe to use it unless you know that something else owns it. You probably want either [coleccionVista release]; coleccionVista = nil; or just self.coleccionVista = nil if you've made it a property.

tc.