退出应用程序时dealloc方法是否正常执行?
当我的应用程序终止时,我使用如下代码(例如在我的 appController.m 中)进行一些清理...应用程序退出
- (void) dealloc {
[myObject release]; // myObject 's dealloc will not be called either !!!
[arraySMSs release];
[super dealloc];
}
时永远不会调用此方法!为什么 ?有更好的地方来清理吗?不调用的事实解决了内存泄漏问题?或者操作系统确实负责清理工作?
谢谢...
I use code like the following (inside my appController.m for example) to do some cleanup when my application terminates...
- (void) dealloc {
[myObject release]; // myObject 's dealloc will not be called either !!!
[arraySMSs release];
[super dealloc];
}
This method never get called when the app quits! Why ? Is there a better place to do my clean up ? The fact that is not called addresses memory-leak issues ? Or the OS does take care of clean up ?
Thank you...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
系统没有理由确保在应用程序终止时单独释放每个对象。
这样做只是浪费CPU周期,浪费用户的时间。
当应用程序终止时,系统会以完全自动且不可避免的方式回收该应用程序使用的所有资源。
如果您需要在应用程序终止时发生某些事情,请使用应用程序委托的挂钩来执行此操作。但不要依赖于此。用户可以随心所欲地强制重新启动设备或强制退出应用程序。
There is no reason for the system to ensure that every object is individually deallocated upon application termination.
Doing so is just a waste of CPU cycles and a waste of the user's time.
When an app is terminated, all resources used by that app are reclaimed by the system in an entirely automatic and unavoidable fashion.
If you need something to happen at app termination, use the application delegate's hooks for doing so. But don't rely on that. A user may force reboot a device or force quit an application at whim.
这是 NSObject Reference 中的引用:
“重要提示:请注意,当应用程序终止时,可能不会向对象发送 dealloc 消息,因为进程的内存在退出时会自动清除 - 简单地允许操作系统清理资源比调用所有内存管理方法更有效”。
这几乎证实了很多人所说的。
Here's the quote from NSObject Reference:
"Important: Note that when an application terminates, objects may not be sent a dealloc message since the process’s memory is automatically cleared on exit—it is more efficient simply to allow the operating system to clean up resources than to invoke all the memory management methods."
It pretty much confirms what many people have said.
好问题,我也很困惑。
现在我得到了这个:
说我们的自定义代码没有管理任何属于 appDelegate 类本身的对象,我们真的不需要担心“释放”它的实例。
UIApplication 是唯一保留它的类,但我们不欠它。
但是,为了学术讨论或者如果有什么我目前不知道的目的,
当您想在 appDelegate 类中测试 dealloc 时:
applicationWillTerminate
是了解您的应用是否将退出的正确位置。nice question, i was confused too.
now i got this:
Said that there's no object managed by our custom code that owes the appDelegate class itself, we don't really need to worry to "release" its instance.
UIApplication is the only class that retain it, but we don't owe it.
But, for academic discussion or if there's any purpose i don't know at the moment,
when you wanna test the dealloc in your appDelegate class:
applicationWillTerminate
is the right place to know if your app is going to quit.是什么让您认为 dealloc 没有被调用?你运行过这个调试器吗?请参阅此问题,了解为什么您不一定能够在 dealloc 方法中调用 NSLog: dealloc什么时候执行?
What makes you think dealloc is not being called? Have you run this is the debugger? Please see this question for why you won't necessarily be able to call NSLog in the dealloc method: when is dealloc executed?