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:
Does the simulator disconnect from Xcode on app exit stopping my NSLog
s echoing to the console?
Are dealloc
s not called as an optimisation, because the app is exiting anyways?
Would the dealloc
s get called when the app is running on actual iPhone hardware?
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).
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.
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With