0

我有以下问题。当我按下后退按钮弹出视图控制器时,不会调用 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 的引用(我一直在代码中搜索,该方法的所有用法都在我向您展示的代码中)。

任何的想法?提前致谢!

4

2 回答 2

0

最后我想我知道发生了什么。我已经更改了很多代码,所以我不确定,但似乎它是一个使用类 coleccionVista 的方法的 NSTimer,所以它正在维护类的引用,所以不可能释放它。

于 2010-08-19T16:50:30.767 回答
0

两个大问题:

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

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

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

你在释放一些东西。你不再拥有它。除非您知道其他人拥有它,否则使用它是不安全的。你可能想要[coleccionVista release]; coleccionVista = nil;或者只是self.coleccionVista = nil你已经把它变成了一个属性。

于 2010-08-19T17:32:09.660 回答