Scene cleanup on application termination

When a cocos2d-x application is terminated by the user on iOS or Android, cocos2d::Director::purgeDirector() and subsequently the running scene’s onExit() and Destructor are not called. I assume this leads to memory leaks which are cleaned up my the OS when the process is killed. Interestingly Desktop builds do actually call cocos2d::Director::purgeDirector() via cocos2d::Director::end().

My current use case is to save game state to file when the application is closed. There is no platform independent ‘applicationWillTerminate’ provided by ApplicationProtocol and applicationDidEnterBackground() is not always called on termination, particularly on Android. So I decided to save state whenever one of my scenes exit. But alas.

I am curious if anyone has noticed this and what work arounds are available. Preferably I’d rather avoid saving state to file every time there is a change.

There was already discussion on implementing applicationWillTerminate few years ago but it is not done yet.
See Add applicationWillTerminate() interface into CCApplication?