cocos2d-x game crashes when entered background
Asked Answered
G

1

6

my cocos2d-x game crashes when entering background. here is some code from AppDelegate:

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{

    CCDirector::sharedDirector()->pause();

    CCUserDefault::sharedUserDefault()->flush();

    CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();

}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{


    CCDirector::sharedDirector()->resume();

    CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

and the error message:

libGPUSupportMercury.dylib`gpus_ReturnNotPermittedKillClient:
0x3797e094:  trap   
0x3797e096:  nop    

note that it always crashes for iPhone, but 99% crashes on Android (okay when the game haven't load large images etc)

EDIT: I've tried CCDirector::sharedDirector()->stopAnimation() and it works great for iOS. But still crashes for Android (not immediately. when returning back to the app, the screen become black (but i think it is still running because background music is still playing. then about 5 seconds later it crashes)

EDIT 2: The error message in Eclipse:

libEGL   call to OpenGL ES API with no current context (logged once per thread)      (red warning text)

libc     Fatal signal 11 (SIGSEGV) at 0x5f012000 (code=2)                  (black text)
Gesticulate answered 13/6, 2012 at 4:15 Comment(0)
L
5

The app delegate method applicationDidEnterBackground: is called after your application transitions to the background, but before your application is suspended. Unfortunately, you may not perform any GPU instructions while in the background, or the watchdog will terminate you (as you're seeing here).

Assuming that your CCDirector::sharedDirector()->pause() call is responsible for stoping your graphics/animation loop, you should move that to the applicationWillResignActive: delegate method. That method is called before your application transitions to the background.

However you have your code structured, make sure your animation loop is completely flushed and stopped before you return from the applicationWillResignActive: delegate call.

Note: This answer is in reference to why it always crashes on iOS

Lacombe answered 13/6, 2012 at 4:22 Comment(4)
i called the ccdirector::sharedDirector()->stopAnimation() and it works nowGesticulate
sorry, crashes for android now. ios is okGesticulate
@Gesticulate I suggest you make two separate questions because the reasons for the crash are going to be different for sure. My guess would be there's another callback in Android where you have to stop updating the GPU but you're not getting the callback.Lacombe
"make sure your animation loop is completely flushed and stopped before you return from the applicationWillResignActive: delegate call." How to do that?Marjorymarjy

© 2022 - 2024 — McMap. All rights reserved.