简体   繁体   中英

Seg fault on Android Cocos2D-x app in lambda callback

I've got a weird crash issue in the Android version of a Cocos2D-X game.

In many places I have callbacks that execute when some other action is completed, invoked like this:

playAnimationOnce(animationName, [this] {
    m_animationStateMachine->changeState(TWITCHING_ANIM_STATE);
    m_stateMachine->changeState(DEAD);
});

On Android, m_animationStateMachine is able to change state just fine, however, making any reference to m_stateMachine is causing a crash, specifically:

signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x3f800044
Stack frame #00 pc 008ed5ec  /data/app/com.bundleID.appname/lib/arm/libcocos2dcpp.so (_ZN12StateMachine11changeStateEPK5StatePKv+52): Routine StateMachine::changeState(State const*, void const*) at /path/to/project/proj.android-studio/app/jni/../../../Platform/Util/StateMachine.cpp:158
Stack frame #01 pc 007ef73c  /data/app/com.bundleID.appname/lib/arm/libcocos2dcpp.so: Routine operator() at /path/to/project/proj.android-studio/app/jni/../../../Source/Actors/Hero.cpp:1291

The game works fine on iOS. I have even stepped through the callback's execution on XCode for iOS and verified that the object is valid on iOS.

I don't have a way to use a debugger on Android, so I can't inspect each variable better than with log statements.


Things I have tried already:

  • Capturing with [&] instead of [this]
  • Making a whole new instance of the StateMachine with just two states, this was also causing a crash.

And a couple other things that I'm forgetting at the moment. Feel free to send me any and all suggestions, I have no idea what could be causing this.

I figured it out.

I still don't know what about Android creates the issue though, so if you do, please let me know.

Essentially, when m_animationStateMachine->changeState is executed, a new animation plays which clears currently running animations, and uses a cocos2d-x method to clear the animation callback.

I believe that was causing all the lines after m_animationStateMachine->changeState to execute in some undefined context.

I fixed this by making the m_animationStateMachine->changeState the last line of the callback, and changing the class' state before playing the new animation.


Posting this as an answer so I can resolve the question in a few days, and maybe this will be helpful to somebody else in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM