退出应用程序时dealloc方法是否正常执行?

发布于 2024-10-18 11:51:11 字数 316 浏览 3 评论 0原文

当我的应用程序终止时,我使用如下代码(例如在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沩ん囻菔务 2024-10-25 11:51:11

系统没有理由确保在应用程序终止时单独释放每个对象。

这样做只是浪费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.

回忆追雨的时光 2024-10-25 11:51:11

这是 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.

薔薇婲 2024-10-25 11:51:11

好问题,我也很困惑。

现在我得到了这个:

说我们的自定义代码没有管理任何属于 appDelegate 类本身的对象,我们真的不需要担心“释放”它的实例。
UIApplication 是唯一保留它的类,但我们不欠它。

但是,为了学术讨论或者如果有什么我目前不知道的目的,
当您想在 appDelegate 类中测试 dealloc 时:

applicationWillTerminate 是了解您的应用是否将退出的正确位置。

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [UIApplication sharedApplication].delegate = nil;
    // after this, the dealloc method of our appDelegate class will be called
}

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.

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [UIApplication sharedApplication].delegate = nil;
    // after this, the dealloc method of our appDelegate class will be called
}
昵称有卵用 2024-10-25 11:51:11

是什么让您认为 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文