为什么我应该在应用程序退出之前释放(因此dealloc)对象? [英] Why should I release (and therefore dealloc) objects just before application quit?

查看:178
本文介绍了为什么我应该在应用程序退出之前释放(因此dealloc)对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个iPhone应用程序中有大量的对象将永远活着,直到应用程序死亡。它们是:应用程序委托,窗口,主视图控制器,可以是导航或tab控制器,其中的所有对象。为什么在地球上我应该释放它们,然后退出宝贵的CPU周期?据我了解,应用程序进程将被终止,因此它的堆,一致或不会也会消失。

There are plenty of objects in each iPhone app witch will live forever till app dies. They are: application delegate, window, main view controller, may be navigation or tab controller, all the objects within them. Why on Earth should I release them just before quit spending precious CPU cycles? As far as I understand, apps process will be terminated, so it's heap, consistent or not will be gone too.

那么我为什么要释放他们呢?苹果的开发人员手册坚持,就像下面的代码示例(从 iPhone开发指南)。

So why should I release them? Apple's dev manuals insist on it like in code sample following (from iPhone Development Guide). It's just first place I've found searching by word dealloc in library.

@implementation HelloWorldAppDelegate
@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

    // Override point for customization after app launch
    MyView *view = [[MyView alloc] initWithFrame:[window frame]];
    [window addSubview:view];
    [view release];
    [window makeKeyAndVisible];
}
- (void)dealloc {
    [window release];
    [super dealloc];
}
@end

NHObject-dealloc 方法:


注意,当应用程序终止时,对象 strong>发送一个dealloc消息,因为进程的内存在退出时被自动清除 - 简单地允许操作系统清理资源比调用所有内存管理方法更有效率。因为这个原因和其他原因,你不应该在dealloc中管理稀缺的资源

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. For this and other reasons, you should not manage scarce resources in dealloc

但是在上面的示例中 dealloc 在当前实现中被调用,如果你的应用程序足够快到在15秒内对applicationWillTerminate做出反应。

But dealloc method in sample above is called in current implementation if your application is fast enough to react to applicationWillTerminate within 15 seconds.

再次。我应该避免在应用程序退出速度上写上面的dealloc方法,或者这种方法有什么问题吗?

So again. Should I avoid writing dealloc method above for app exit speed or are there any problems with such approach?

推荐答案

Artem。

让我们玩一个名为what if的小游戏。

Let's play a little game called "what if".

,你开发一个伟大的应用程序。一个杰出的应用程序,真的 - 和公众喜欢它!你的销售膨胀,鸟儿在天空中唱你的名字!

What If, you develop a great app. A masterful app, really - and the public loves it! Your sales swell, birds sing your name awing in the sky!

但是,你选择不在你的AppDelegate dealloc中做任何事情。只是几秒钟,你决定不打扰。有什么危害?

But, you chose not to do anything in your AppDelegate dealloc. Just a few seconds of time, you decided not to bother with. What's the harm?

哦,一开始你在一堆钱上睡觉。但随着销售的增长和增长,一点点潮流来到你。然后梦想开始。

Oh at first you sleep easy atop the piles of money. But as sales grow and grow, a little twinge comes upon you. And then the dreams start.

方形形状,首先模糊不清。天过去,他们变得更清楚,因为你睡得越来越少。最后,有一天,你看到了。

Square shapes, indistinct at first. Days pass and they grow clearer, as you sleep less and less. Then finally, one day, you see.

他们是块,Artem。 内存块。他们在你的梦想做什么?好吧,你看,在应用程序退出之前,并没有完全摆脱存在,他们不得不去某个地方。应用程式已离开,手机已移动。

They are blocks, Artem. Memory blocks. And what are they doing in your dreams? Well you see, not quite freed from existence before the application quits, they had to go somewhere. The app was gone, the phone had moved on.

因此他们进入您的 HEAD

我希望这已经丰富了... Artem。

I hope this has been informative... Artem.

这篇关于为什么我应该在应用程序退出之前释放(因此dealloc)对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆