Is denit() guaranteed to be called if the app force closed by user (i.e. user clears it from task screen) or suspend by OS

I have wrestled this question a lot, could not find a concrete answer. I have a singleton(audio player), that I must persist throughout app. My app plays audio, which makes it difficult to predict when app is terminated, since it plays audio in background. Inside my singlton deinit(), I have to do clean i.e release any bufferes associated with my player.


So to summarize what I am asking is if an object always has reference count of 1, will denint() still be called?

Accepted Reply

No, when an app is terminated, no objects are deallocated — all memory contents belonging to the app are just discarded.


It is the system's responsibility to clean up any dependencies of other processes not belonging to your app. For example, if audio is playing, the system is responsible for stopping the audio, and breaking any dependencies on information you app provided.


This is true whether the app terminates itself, or is terminated forcibly by the system.


Note that in those rare cases where your app wants to clean up something external to itself (delete some big files, or close a session with a web server, or deactivate some custom hardware), you have to use one of the application delegate method to do the cleanup before termination. However, if your app is forcibly terminated, you don't even get to do that.

Replies

No, when an app is terminated, no objects are deallocated — all memory contents belonging to the app are just discarded.


It is the system's responsibility to clean up any dependencies of other processes not belonging to your app. For example, if audio is playing, the system is responsible for stopping the audio, and breaking any dependencies on information you app provided.


This is true whether the app terminates itself, or is terminated forcibly by the system.


Note that in those rare cases where your app wants to clean up something external to itself (delete some big files, or close a session with a web server, or deactivate some custom hardware), you have to use one of the application delegate method to do the cleanup before termination. However, if your app is forcibly terminated, you don't even get to do that.

clear concise explanations Thank you!