Re: what are the scenarios for an app to be terminated?
Re: what are the scenarios for an app to be terminated?
- Subject: Re: what are the scenarios for an app to be terminated?
- From: Jack Nutting <email@hidden>
- Date: Thu, 7 Oct 2010 09:11:25 +0200
On Wed, Oct 6, 2010 at 9:13 PM, eveningnick eveningnick
<email@hidden> wrote:
> And what is the right way to terminate application, so it would
> release all its allocated objects? Maybe it's a wrong way to use
> -dealloc also as a destructor (like i did in C++) - where i save
> config file? what is the right way?
>
Your intuition that doing things the C++ way might be wrong, is
correct; Typically, when an application exits, the objects in the
application will *not* be deallocated. Instead, any object that needs
to save some state or do other cleanup with the application is being
exited is to sign up for a notification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myCleanupMethod:)
name:NSApplicationWillTerminateNotification
object:NSApp];
Then put your cleanup code in a method like this:
- (void)myCleanupMethod:(NSNotification *)n {
...
}
If you're used to the idea of the whole stack unwinding and all
destructors being called in a C++ application as it exits, the Cocoa
way of doing this may seem strange, but it's a lot more efficient,
since the application doesn't have to run all that destructor code as
it's exiting, and for the relatively rare cases where an object really
needs to do something as the app is going down, you can sign up for
the notification.
--
// jack
// http://nuthole.com
// http://learncocoa.org
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden