Careful. You are calling CallStaticBooleanMethod on a function that returns void. This should be CallStaticVoidMethod.
While it is non-standard to call exit(), not calling exit() and letting the program flow out of main() ensures that it will re-launch through main with all of your globals set. So, I am self-answering my earlier question: that is how to reproduce the bug ? let the program flow out of main().
While it would be nice not to have global state, sometimes you are tasked with porting a codebase that has globals.
I am working on implementing a proxy .so library that calls the game .so with dlopen() and dlclose(), ensuring the data segment is zeroed. I will report back.
Collecting some code lines how it works for me:
Quit command that is called from the UI or in-game console:
https://github.com/mgerhardy/caveexpress/blob/master/src/modules/server/commands/CmdQuit.h#L9 https://github.com/mgerhardy/caveexpress/blob/master/src/modules/server/commands/CmdQuit.h#L9
Fires a SDL_QUIT event
https://github.com/mgerhardy/caveexpress/blob/master/src/modules/server/SDLBackend.cpp#L98 https://github.com/mgerhardy/caveexpress/blob/master/src/modules/server/SDLBackend.cpp#L98
The SDL_QUIT event is forwarded to some system specific implementation.
On Android this looks like this:
caveexpress/src/modules/common/ports/Android.cpp at master · mgerhardy/caveexpress · GitHub https://github.com/mgerhardy/caveexpress/blob/master/src/modules/common/ports/Android.cpp#L503
And the java side for this is:
caveexpress/android-project/src/org/base/BaseActivity.java at master · mgerhardy/caveexpress · GitHub https://github.com/mgerhardy/caveexpress/blob/master/android-project/src/org/base/BaseActivity.java#L236
This will pause the app via SDL events (halting the main loop).
You should never call exit.
Hope that helps.
On Fri, Oct 9, 2015 at 2:21 PM, Owen Alanzo Hogarth <gurenchan at gmail.com <mailto:gurenchan at gmail.com>> wrote:
I ran into something similar when trying to have this current version of sdl work as a live wallpaper which can have up to 2 instances of the engine running at once.
To me it seems that the way android works just causes some issues with SDL.
I’ve personally seen that if you open android multitasking window and swipe away your sdl app, it dies but the last call you get is the onpause. None of the other sdl calls are made, such as entering background, etc.
There are other ways to kill the sdl activity on android that could cause sdl to act in undefined manner when the app is relaunched.
as it stands sdl works on android but there seems to be a lot of edge cases that needs to be tested. What you are describing is one of them.
I don’t actually think there’s a lot of android developers on this list although I could be mistaken but most of my android specific questions go unanswered.
On Fri, Oct 9, 2015 at 9:11 AM, Michael Labbe <@Michael_Labbe mailto:Michael_Labbe> wrote:
Thread resurrect.
Okay, so we know that main() can be called multiple times on Android, and that the global variables in the game’s shared object are still set. However, in order to debug this situation, it is important to be able to create the situation on the fly.
How do I force my application to be backgrounded in such a way that the activity is destroyed so that I can re-enter through main()?
On Wed, Sep 23, 2015 at 7:08 PM, Jonathan Dearborn <grimfang4 at gmail.com <mailto:grimfang4 at gmail.com>> wrote:
It indeed does break conceptions about main(), and that is unfortunate. It’s a platform issue because the Java side loads your native code as a dynamic library and never unloads it (and I don’t think any API to unload it exists on Android). So when the Java code calls into your main() a second time, the static (and global?) variables are already set.
You can avoid an Android-only code section with a custom generic quit() that resets your static variables to their initial values.
Jonny D
On Wed, Sep 23, 2015 at 9:08 PM, Michael Labbe <@Michael_Labbe mailto:Michael_Labbe> wrote:
So, what we’re saying is that it’s possible and reasonable, on Android, to have the SDLThread be killed while the app is in the background, and to have it call main() a second time if the app is re-opened?
That’s completely unlike any of the other platforms SDL2 supports and supporting that seems like a very strange requirement. It breaks the main() metaphor entirely.
Just making absolutely sure I am not putting an unnecessary android-specific piece of code in to skip all subsystem and engine initialization the second time it goes through main and that this is intended by design?
This is definitely happening on a wide-ish range of Android devices in our testing, by the way. We are seeing re-entry crashes (thanks to Crashlytics) on Android major version 4 and 5 and across device brands.
On Tue, Sep 22, 2015 at 10:10 AM, Jonathan Dearborn <grimfang4 at gmail.com <mailto:grimfang4 at gmail.com>> wrote:
So, yes, there is a reason. I’m not totally up on it, but Eric Wing pointed out to me that since the native code is managed by Java via .so loading, when an app resumes from the background without having its process killed, static variables may retain their old values. You must de-initialize these before your app exits to ensure that running your initialization again will work correctly.
Jonny D
On Tue, Sep 22, 2015 at 12:55 PM, Michael Labbe <@Michael_Labbe mailto:Michael_Labbe> wrote:
Being able to quit the application with the back button is a given for proper UI conformance, but that doesn’t solve reentry for more complicated scenarios such as returning to an app after responding to an email activity.
Also, re-entering through main() in Android is totally incongruous with iOS app delegate responder messages (ex: applicationDidEnterForeground). It would be best to figure out why the app is re-entering in main() instead of triggering the specific event and deal with that.
My key question remains:
Is there any reason why an Android app using SDL would enter main() more than once, but have all global variables still be initialized?
On Mon, Sep 21, 2015 at 2:43 AM, Owen Alanzo Hogarth <gurenchan at gmail.com <mailto:gurenchan at gmail.com>> wrote:
Here’s how i’d recommend exiting your application.
void handle_event(SDL_Event* e)
{
if (e->type == SDL_KEYDOWN)
if (e->key.keysym.sym == SDLK_AC_BACK )
{
do your quitting code here
}
…
I wouldn’t recommend using event filters, why? I do mainly android dev and never had to use it, maybe others on this list will have different opinions though.
There was some discussion on this thread earlier about using exit(0) or some other method to break your loop instead.
On Mon, Sep 21, 2015 at 4:53 PM, Martin Gerhardy <martin.gerhardy at gmail.com <mailto:martin.gerhardy at gmail.com>> wrote:
How do you exit your application?
Am 21.09.2015 um 10:33 schrieb Michael Labbe:
My event handling code calls this at startup:
SDL_SetEventFilter(HandleAppEvents, NULL);
And then, HandleAppEvents returns 0 (handles and eats the event for):
- SDL_APP_TERMINATING
- SDL_APP_DIDENTERBACKGROUND
- SDL_APP_DIDENTERFOREGROUND
It does not explictly handle, and returns 1, for all other events.
… but regardless, why is main() being called twice in one launch of the app? Once at startup, and once on resume from background.
On Mon, Sep 21, 2015 at 12:58 AM, Owen Alanzo Hogarth <gurenchan at gmail.com <mailto:gurenchan at gmail.com>> wrote:
can you show your event handling code?
On Mon, Sep 21, 2015 at 2:04 PM, Michael Labbe < mailto:Michael_Labbe@Michael_Labbe mailto:Michael_Labbe> wrote:
I’m dealing with my app being loaded from the background. The vast majority of the time, onResume is called. I catch that event and deal with it in native code. Great – everything’s fine.
Sometimes, onResume is not called and I end up in my main() function. However, all of my globals are still initialized, so the application state seems to be present.
I have partial repro steps on this by:
- launching my game
- pressing the home button
- tapping my game’s icon
- since my app is now in a strange state, rerunning init code crashes it pretty quickly in my own code.
I’m using 2.0.4RC1.
What is Android doing here?
Michael Labbe
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
–
http://www.caveproductions.org http://www.caveproductions.org/
http://www.ufoai.org http://www.ufoai.org/
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
–
http://www.caveproductions.org http://www.caveproductions.org/
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org