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
NSLogs echoing to the console? -
Are
deallocs not called as an optimisation, because the app is exiting anyways? -
Would the
deallocs get called when the app is running on actual iPhone hardware?
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,
deallocmay not be sent to a vast number of objects; and for this reason it is important not to manage scarce resources indealloc. To clean up scarce resources it is probably a better idea to have your application delegate respond toapplicationWillTerminate:and do your cleanup in there.