Android application crashes when relaunching
I had originally asked the question here:
http://www.experts-exchange.com/Programming/Languages/Java/Android/Q_26754261.html But I guess this might be a better place to ask the question.I have made an Android application that calls some native code. When I first run the application on an Android 2.2 device (by pressing the icon) it works fine but if I press the back button and then relaunches the application by pressing its icon again, the application crashes (info from eclipse):
01-19 13:21:51.989: INFO/DEBUG(30): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
01-19 13:21:51.989: INFO/DEBUG(30): Build fingerprint: 'generic/sdk/generic:2.3/GRH55/79397:eng/test-keys'
01-19 13:21:51.989: INFO/DEBUG(30): pid: 354, tid: 381 >>> Test.Application <<<
01-19 13:21:51.989: INFO/DEBUG(30): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0000005a
I have checked the native code and don't see any bugs. The code also works the first time I run it. It has something to do with that not all resources are freed when pressing the back button. I though I had found a simple solution to the problem by inserting System.exit(0) in onDestroy() but this only works when the application is in landscape mode.
In landscape mode the life cycle is like this:
01-15 22:38:29.913: INFO(18062): onCreate
01-15 22:38:30.143: INFO(18062): onResume
Pressed back button
01-15 22:38:34.093: INFO(18062): onPause
01-15 22:38:34.343: INFO(18062): OnDestroy
In portrait mode the life cycle is like this:
01-15 22:41:00.343: INFO(18167): onCreate
01-15 22:41:00.863: INFO(18167): onResume
01-15 22:41:00.913: INFO(18167): onPause
01-15 22:41:00.913: INFO(18167): OnDestroy
01-15 22:41:01.423: INFO(18167): onCreate
01-15 22:41:01.693: INFO(18167): onResume
Pressed back button
01-15 22:41:05.663: INFO(18167): onPause
01-15 22:41:05.963: INFO(181开发者_Python百科67): OnDestroy
So in portrait mode things happen twice - so if I insert System.exit(0) in onDestroy the application will not launch when the device is upright/in portrait mode.
I guess the best solution would be to find the real cause to why the native code is crashing but so far I have not found the cause. Maybe it has something to do with low memory. I have tried running Debug.getNativeHeapFreeSize() and the amount of free memory is very low at times but the error message says "fault address".
After the application has crashed I press its icon and run the application again without problems. So the application only successfully launches half of the times you press its icon.
Do you have any idea what is wrong or perhaps a workaround? I have searched for an answer on this site and found some information about the double life cycle problem and how to get a JNI stack trace. I will look some more at it and see if I can find the cause. I could also make an ugly workaround that ensures System.exit(0) is not called the first time onDestroy is called if the device is in portrait mode but if the application then is installed on a device that does not have this double life cycle problem the application will not shut down properly when in portrait mode.
/Kim
It actually was because I forgot to free some memory in the C++ code that this error occurred. Now it finally does not crash anymore and the double life cycle issue does not matter anymore.
精彩评论