使用 navigationController 弹出 viewController 时释放内存的问题
我遇到以下问题。当我按下后退按钮弹出视图控制器时,不会调用 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 版本] 之后强制执行另一个 [coleccionVista 版本],则会显示 dealloc 消息,但在尝试 [super dealloc] 时崩溃。我没有持有 coleccionViewController 的任何其他引用(我一直在代码中搜索,该方法的所有用法都在我向您展示的代码中)。
有什么想法吗?提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后我想我明白发生了什么。我已经改变了很多代码,所以我不确定它,但它似乎是一个使用类 coleccionVista 的方法的 NSTimer,所以它维护了该类的引用,所以不可能取消分配它。
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.
两个大问题:
大概
coleccionVista
可能是非零,但在分配一个新的之前您没有释放它。这要么是泄漏,要么是崩溃。另请注意,-retainCount 返回 NSUInteger,而不是 int;使用 %i 对其进行格式化会调用未定义的行为(通常这只是在大端 64 位系统上打印错误的数字)。你正在释放一些东西。你不再拥有它。除非您知道其他东西拥有它,否则使用它是不安全的。您可能需要
[coleccionVista release]; coleccionVista = nil;
或者只是self.coleccionVista = nil
(如果您已将其设为属性)。Two big problems:
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).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 justself.coleccionVista = nil
if you've made it a property.