0

I'm not using ARC and am a bit confused about what happens to all the objects I've retained when an app becomes suspended.

I had a look at this thread but it didn't help that much:

iPhone. Shouldn't hitting the home button cause UIApplicationDelegate's dealloc to be called

I've read the apple docs on multitasking but couldn't find any information specifically about dealloc.

I ran instruments, which didn't find any leaks but I'm unclear on what happens when the stop button is pressed

So my questions are:

1) Why aren't some of my dealloc methods being called? For example, the dealloc for my UIResponder is not being called (when I double tap the home button on my iPad and remove my app) and neither are the ones for any of my UIViewController objects.

2) When I hit the stop button on Instruments (while it's running checking for leaks), what mode is the app placed in? (for e.g inactive, background, suspended....something else?)

Since my mind is too accustomed to the C++ way of thinking where an application exit results in destructors being called, it's hard for me to visualise the iOS equivalent.

iOS deployment target: 8.0

Community
  • 1
  • 1
Ash
  • 2,021
  • 2
  • 26
  • 59
  • I found these threads that helped: http://stackoverflow.com/questions/5079563/does-dealloc-method-being-executed-normally-when-quitting-the-application http://stackoverflow.com/questions/1533505/why-should-i-release-and-therefore-dealloc-objects-just-before-application-qui http://stackoverflow.com/questions/27523768/is-dealloc-always-called-even-when-you-swipe-close-an-app – Ash Oct 20 '15 at 06:20

1 Answers1

3
  1. Your app is simply sent to the background. It's still in memory. If the user brings your app back up, it's right where it left off. It would be bad if anything was deallocated for no reason just because it wasn't in the foreground. When a user double-taps the Home button and removes the app, the app is killed hard. See #2.
  2. When you hit the Stop button in Xcode (or the user terminates the app by double-pressing the Home button) your app is killed. The app process is gone. All memory and other resources are released. This is the sledgehammer. Your app is terminated with no notification of any kind. Imagine doing kill -9 from the command line.

None of this has anything to do with using ARC or not. None of it has to do with using Objective-C or C++ or Swift.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks rmaddy. What then is the point of dealloc? When is it called? – Ash Oct 20 '15 at 05:53
  • It's the same as a destructor in C++. It's called when there are no more references to the object. It could a simple string going out of scope or a view controller being dismissed. – rmaddy Oct 20 '15 at 05:54
  • Yes, so at what point in the app's lifecycle will there be no references to the UIResponder and the UINavigationController objects it keeps references to? – Ash Oct 20 '15 at 05:55
  • It has nothing to do with the app's lifecycle. It has to do with reference counting. An object is in memory until it has no references, then it gets deallocated. For a navigation controller, it depends on how it's used. If it's the app's root view controller, it will never be deallocated. If it is presented and then dismissed, it will be deallocated when it is dismissed. – rmaddy Oct 20 '15 at 05:57
  • I see. Can the OS ever hard kill an app? (lack of memory?). My understanding so far is that a hard kill wipes clean all memory used by the app and deallocs for your navigation controllers are not called. Is that correct? – Ash Oct 20 '15 at 06:03
  • ...because hard-killing affects lifecycle which has nothing to do with reference counts (and therefore deallocs). – Ash Oct 20 '15 at 06:08
  • Anytime your app is in the background it can be hard killed by the OS. Navigation controllers can be deallocated. See my previous comment. – rmaddy Oct 20 '15 at 06:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92809/discussion-between-ash-and-rmaddy). – Ash Oct 20 '15 at 06:10