iPhone:在parentViewController上调用dealloc会导致异常
我正在处理 viewDidUnload 和 dealloc 方法,并且在调用 [super dealloc] 时发现了一个问题;在父视图控制器中。
我有很多带有自定义代码的视图控制器,我已将它们放在父视图控制器的外部。因此,在定义视图控制器时,我设置了对超类的引用:
@interface LoginViewController : AbstractViewController
然后,在 dealloc 方法中,我调用 AbstractViewController dealloc 方法:
//(Login View Controller code)
- (void)dealloc {
[user release];
[passwd release];
[super dealloc];
}
[super dealloc] 执行以下代码:
//(Abstract View Controller code)
- (void)dealloc {
[dbUtils release];
[loadingView release];
[super dealloc];
}
如果我在 iPhone Simulator 上模拟内存警告,则以下内容抛出异常:
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
但是,如果我注释 AbstractViewController 中的 [super dealloc] 行,则不会抛出异常,并且我的应用程序仍在运行。
再次感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您正在释放视图控制器,但随后尝试再次使用它。当您由于内存警告而释放视图控制器时,请记住将指针设置为 nil,这样您就不会意外地再次使用它。即类似
或,如果 myLoginViewController 是一个属性,您可以以更简洁的方式执行此操作:
希望有所帮助,
Sam
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
or, if myLoginViewController is a property you can do this in a neater way :
Hope that helps,
Sam
注意 -didReceiveMemoryWarning 不会触发 -dealloc,它会触发 -viewDidUnload。所以我猜你的 -viewDidUnload 实现做了一些错误,导致控制器的最终保留被释放,所以 -dealloc 被调用。我刚刚在我的代码中遇到了这个问题,这是由 -viewDidUnload 中释放的保留回收引起的。
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.
这发生在我身上,花了一段时间才弄清楚。其中一位代表试图发送一条消息,因为该代表是自己,并且这是一个强有力的参考。当我让它变弱时,它似乎已经修复了。
This happened to me and took a while to figure out. One of the delegates was trying to send a message because the delegate was self and it was a strong reference. when I made it weak it seems to have fixed it.