4

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

Vassilis
  • 2,878
  • 1
  • 28
  • 43
  • What is your target: Mac OS or iOS? There are better places to do termination cleanup depending on your target. –  Feb 22 '11 at 15:01
  • Documentation stating that `-dealloc` may not be sent upon application termination: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html –  Feb 22 '11 at 15:29
  • @Bavarious My Target is Mac OS – Vassilis Feb 22 '11 at 17:55

4 Answers4

11

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.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • All the objects to be released (or other operations) should be visible to app's delegate object right? Isn't it a bit strange? – Vassilis Feb 22 '11 at 17:27
  • 1
    No -- it isn't strange at all. There is *no point* in the application making sure memory is free()d prior to termination. It, quite literally, is a complete waste of time & battery life to do so. – bbum Feb 22 '11 at 18:02
3

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
}
meronix
  • 6,175
  • 1
  • 23
  • 36
3

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.

-1

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?

Community
  • 1
  • 1
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
  • I have added break points, etc .. but never passes through! I' ve just read the answer you linked. Xmmm Interesting! I will try to call a "custom clean up method". I am releasing a Thread in my -dealloc, that's why I am curious! I want to be sure that is cancelled and released. Any ideas ? – Vassilis Feb 22 '11 at 15:01
  • Well upon exit dealloc is definitely called, and even if it weren't, the kernel should reclaim memory of a killed process – ennuikiller Feb 22 '11 at 15:16
  • Upon application termination *there is no guarantee that `dealloc` is called*. This is documented in `NSObject` class reference. –  Feb 22 '11 at 15:21
  • So far I can say that "the runtime system may simply release all of the remaining memory in a single shot, without bothering to call the dealloc methods." as @e.James said. But what if for some reason we want to make `dealloc` be executed by force ? any alternatives ? – Vassilis Feb 22 '11 at 15:47