We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2679

Summary: Crash resuming from screen off
Product: SDL Reporter: John McFarlane <john>
Component: *don't know*Assignee: Gabriel Jacobo <gabomdq>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: critical    
Priority: P2    
Version: don't know   
Hardware: ARM   
OS: Android (All)   
Attachments: Wait on the wrapper thread instead of the native thread

Description John McFarlane 2014-08-10 18:15:14 UTC
Intermittend dereference of mSingleton in SDLActivity.handleNativeExit crashes Android SDL app when mSingleton is null. This seems to be caused by a race between main thread (call to initialize in onCreate) and listener thread created in SDLActivity.surfaceChanged. May also depend on how quickly app responds to SDL_QUIT/SDL_APP_TERMINATE or SDL_APP_WILLENTERBACKGROUND.

Suggested fix can be found at: https://code.google.com/p/crag/source/diff?spec=svn409a3a0bc878362abb80efeaf97ff28917b22894&name=develop&r=409a3a0bc878362abb80efeaf97ff28917b22894&format=side&path=/android/src/org/libsdl/app/SDLActivity.java although there may be a problem with differen SDLActivity instances being held simultaneously. (I'm not very familiar with Android Activities.)

Steps to reproduce may vary with device and system and happen on my HTC One (HTC Android 4.4.3). I believe the same thing may be happening on Samsung Galaxy S5.
1. Start SDL app
2. Press power button to turn device screen off
3. (Possibly required) Press power button again and revive application

Repro rate: ~20%

Note: I'm not sure how pertinent but when device goes into sleep mode, my app responds to SDL_QUIT / SDL_APP_TERMINATE by quitting SDL_main function. However, onCreate is immediately called again and SDL_main function is called. This happens when screen is turned off with power button - not when app is revived. This seems like an innapropriate place for onCreate to be called. Not sure if this is an issue with SDL, Android or HTC.
Comment 1 Gabriel Jacobo 2014-09-16 14:08:55 UTC
Created attachment 1868 [details]
Wait on the wrapper thread instead of the native thread

Let me know if the attached patch solves the problem.
Comment 2 John McFarlane 2014-09-16 15:47:05 UTC
Patch causes compiler errors: 

-compile:
    [javac] Compiling 1 source file to /home/john/crag/android/bin/classes
    [javac] /home/john/crag/android/src/org/libsdl/app/SDLActivity.java:746: error: local variable sdlThread is accessed from within inner class; needs to be declared final
    [javac]                         sdlThread.join();
    [javac]                         ^
    [javac] /home/john/crag/android/src/org/libsdl/app/SDLActivity.java:756: error: incompatible types
    [javac]             }).start();
    [javac]                     ^
    [javac]   required: Thread
    [javac]   found:    void
    [javac] Note: /home/john/crag/android/src/org/libsdl/app/SDLActivity.java uses or overrides a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] 2 errors

Tried these counter-patches:
-            SDLActivity.mSDLThread = new Thread(new SDLMain(), "SDLThread");
+            final Thread sdlThread = new Thread(new SDLMain(), "SDLThread");
and
-            }).start();
+            });
+            SDLActivity.mSDLThread.start();

Which appears to work! This also seems to address 2703. Many thanks.
Comment 3 Gabriel Jacobo 2014-09-17 12:00:20 UTC
http://hg.libsdl.org/SDL/rev/c197a316685a