How to cleanup on 4.1 devices?

I’ve noticed that the examples don’t call testApp::exit on 4.1 devices compiled under 4.1 SDK when the user quits the app. You can see this in the simulator as well.

It turns out that when the user presses the front panel close button on the device, applicationWillResignActive in is called, as opposed to applicationWillTerminate.

As such, you just get a loss of focus event and XCode thinks the app is still running, and no cleanup code is called.

A hacky workaround is to just add exit(0); in applicationWillResignActive if you just want the app to cleanup and fully terminate, but I’d like to know the correct way to handle this?

Issue occurs on github master and memo’s github version as well. Does not occur if you build for 3.2 device.

XCode: 3.2.4
OF: github master @24 Sep 2010
Compiled as: device base SDK 4.1 release deployment target 3.2

thanks, David

Hey David I just responded to your issue on github, but I may as well copy it over here since I was bashing my head against this last night.

The reason that applicationWillTerminate is not called is because the app is actually still running, just in the background. If you do a shutdown using the task manager (the thing that pops up at the bottom of the screen when you double click the home button), the app is not guaranteed to send an applicationWillTerminate either.

Unfortunately, there doesn’t seem to be a really good way to clean up all your malloc’d memory.

If you are using the exit function for app state serialization (like me), this may bite you pretty hard, so it is worth adding a call to your save function inside either applicationWillResignActive or applicationDidEnterBackground. I only do saving inside of my exit, so I called that from applicationDidEnterBackground. Probably not the best solution, but it worked for me.

Thanks Jonathan. I’m glad it’s not just me, anyway. I thought I might have some weird build issue.

Howdy –

Just found this thread on the topic.…-s-4-a.html

Couple of interesting points:

– some folks call exit() for phones running iOS3
– others clear the memory in applicationDidEnterBackground assuming it’ll never be opened again (and presumably reallocate when it does)
– it’s messed up that apple simple terminates your app when a user quits it from the list of active apps

Still not exactly sure how to handle the situation,


Great link thanks mantissa. Yes, it’s hard to get your head around it. It’s a weird feeling knowing your application has quit and yet not quite quit at the same time. Makes me wonder at the overhead from all the other not-quite-quit applications apps in memory.

Anyway for me the simplest approach was to save settings and dump all allocated memory on lostFocus() and reverse that on gotFocus(). That way I don’t have to mess about under the hood more than I have to, and makes my app super polite when interrupted. I guess it depends on your app though - if it had large setup/cleanup times or difficult state restoral then applicationDidEnterBackground would be the way to go.