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 1903

Summary: SDL_GL_SwapWindow interacting badly with app inactivity
Product: SDL Reporter: Wouter van Oortmerssen <aardappel>
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED INVALID QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: amaranth72
Version: HG 2.0   
Hardware: iPhone/iPod touch   
OS: iOS 6   

Description Wouter van Oortmerssen 2013-06-07 15:49:41 UTC
Upon pressing the home button I occasionally get this crash:

#0	0x375dee2e in gpus_ReturnNotPermittedKillClient ()
#1	0x375df764 in gpusSubmitDataBuffers ()
#2	0x32d735e4 in SubmitPacketsIfAny ()
#3	0x351d237a in gliPresentViewES ()
#4	0x351dadf2 in -[EAGLContext presentRenderbuffer:] ()
#5	0x0039186a in EAGLContext_presentRenderbuffer(EAGLContext*, objc_selector*, unsigned int) ()
#6	0x0016c55e in -[SDL_uikitopenglview swapBuffers] at /Users/aardappel/lobster/external/SDL-2.0.0-7046/Xcode-iOS/SDL/../../src/video/uikit/SDL_uikitopenglview.m:199
#7	0x001695cc in UIKit_GL_SwapWindow at /Users/aardappel/lobster/external/SDL-2.0.0-7046/Xcode-iOS/SDL/../../src/video/uikit/SDL_uikitopengles.m:92
#8	0x001679b0 in SDL_GL_SwapWindow at /Users/aardappel/lobster/external/SDL-2.0.0-7046/Xcode-iOS/SDL/../../src/video/SDL_video.c:2739

I say occasionally, because it appears to depend on the device I am running on, and general timing wether it happens or not. It is not easy to reproduce.

Googling makes it obvious is that the problem is related to calling OpenGL functions while in the background: https://github.com/BradLarson/GPUImage/issues/728

So clearly this is my fault, I should be tracking SDL_WINDOWEVENT_MINIMIZED and suspending any OpenGL calls during that time, in particular, not call SDL_GL_SwapWindow. When I do that, it stops crashing when I press the home button, but it starts crashing with almost the same callstack upon resuming the app, and in my case, ONLY when not run from under Xcode (but directly running the last build on the device). This may again be a timing issue.

0   libsystem_kernel.dylib        	0x3bc0f838 __kill + 8
1   lobster_ios                   	0x001d9564 SDL_Parachute (SDL_fatal.c:42)
2   libsystem_c.dylib             	0x3bb90e90 _sigtramp + 40
3   libGPUSupportMercury.dylib    	0x370c1754 gpusSubmitDataBuffers + 120
4   IMGSGX543GLDriver             	0x3288cf2c SubmitPacketsIfAny + 240
5   GLEngine                      	0x34cdd300 gliPresentViewES + 204
6   OpenGLES                      	0x34ce5dd6 -[EAGLContext presentRenderbuffer:] + 74
7   lobster_ios                   	0x0021555c -[SDL_uikitopenglview swapBuffers] (SDL_uikitopenglview.m:199)
8   lobster_ios                   	0x002125ca UIKit_GL_SwapWindow (SDL_uikitopengles.m:92)
9   lobster_ios                   	0x002109ae SDL_GL_SwapWindow (SDL_video.c:2739)

So basically, we have received SDL_WINDOWEVENT_RESTORED, but it still crashes in the next SDL_GL_SwapWindow because we're not supposed to use GL functionality? I experimented with delaying the call to SDL_GL_SwapWindow with SDL_Delay, and that appears to help, but I can't tell for sure seeing how hard to reproduce this is.

So I guess:
- at the very least, the correct way to deal with SDL_WINDOWEVENT_MINIMIZED and any SDL/GL calls should be documented, if there is one.
- ideally SDL would handle this internally, i.e. if SDL_GL_SwapWindow internally decides to not run when in the background on iOS, that probably be simpler for everyone.. SDL already suspends my mainloop os IOS, so this could make it fully transparent for a ported desktop game. Though if you do that, syncing it with SDL_WINDOWEVENT_MINIMIZED / SDL_WINDOWEVENT_RESTORED is apparently not good enough given that it can still crash afterwards. Why, I have no idea yet.
Comment 1 Alex Szpakowski 2013-06-08 00:51:11 UTC
Have you tried stopping GL/SwapWindow calls on SDL_APP_WILLENTERBACKGROUND and resuming on SDL_APP_DIDENTERFOREGROUND, instead of the minimization events?
Comment 2 Wouter van Oortmerssen 2013-06-08 23:11:01 UTC
That fixes it.. hadn't seen this new feature, thanks!