使用navigationController弹出viewController时释放内存的问题

时间:2010-08-17 08:42:42

标签: ipad uinavigationcontroller release dealloc pushviewcontroller

我遇到以下问题。当我按下后退按钮弹出视图控制器时,不会调用dealloc方法。

以下是我正在使用的代码:

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]);

我在控制台上收到这些消息:

我第一次推动视图:

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

第二次:

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

我还在dealloc方法上显示了一条未显示的NSLog消息。但我注意到,如果我在另一个之后强制另一个[coleccionVista发布],则会显示dealloc消息,但在尝试[super dealloc]时崩溃。我没有持有coleccionViewController的任何其他引用(我一直在搜索代码,方法的所有用途都在我向你展示的代码中)。

有什么想法吗?提前谢谢!

2 个答案:

答案 0 :(得分:0)

最后我想我弄明白发生了什么。我已经改变了很多代码,所以我不确定它,但似乎它是一个使用类coleccionVista的方法的NSTimer,所以它维护了类的引用所以它是不可能的解除分配。

答案 1 :(得分:0)

两个大问题:

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

据推测,coleccionVista可能是非零的,但在分配新的之前你不会发布它。这可能是泄漏或崩溃。另请注意-retainCount返回NSUInteger,而不是int;使用%i格式化它会调用未定义的行为(通常这只是在big-endian 64位系统上打印错误的数字)。

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

你正在释放一些东西。你不再拥有它。使用它是不安全的,除非您知道其他人拥有它。如果您已将其设为属性,则可能需要[coleccionVista release]; coleccionVista = nil;self.coleccionVista = nil

相关问题