Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does dealloc get called on iPhone?

I have noticed whilst learning how CocoaTouch works over the last few weeks that dealloc methods don't seem to be getting called when I exit the app in the iPhone Simulator. Now, to be fair, I am not doing anything too scientific - just putting in NSLog statements that can print to the console. My questions are:

  1. Does the simulator disconnect from Xcode on app exit stopping my NSLogs echoing to the console?

  2. Are deallocs not called as an optimisation, because the app is exiting anyways?

  3. Would the deallocs get called when the app is running on actual iPhone hardware?

like image 497
fuzzygoat Avatar asked Feb 11 '10 23:02

fuzzygoat


People also ask

What does dealloc do?

The DEALLOC operation frees one previous allocation of heap storage. pointer-name is a pointer that must be the value previously set by a heap-storage allocation operation (either an ALLOC operation in RPG, or some other heap-storage allocation mechanism).

What is Dealloc in Objective C?

It's called when the reference count for that object becomes 0 because all its pointers have been released. The memory taken up by it is deallocated (freed); the object itself is destroyed.


1 Answers

The docs say that it is more efficient for the operating system to reclaim the memory all at once than for the application to slowly relinquish all of its chunks of memory. For this reason, dealloc may not be sent to a vast number of objects; and for this reason it is important not to manage scarce resources in dealloc. To clean up scarce resources it is probably a better idea to have your application delegate respond to applicationWillTerminate: and do your cleanup in there.

- (void) applicationWillTerminate:(NSApplication/UIApplication *) anApp
{
    [scarceResourceManager relinquishScarceResources];
}
like image 179
dreamlax Avatar answered Sep 21 '22 14:09

dreamlax