0

I'm facing with a lot of crash when try to addChild, removeChild inside hardware interrupts, or JAVA callback.

My game has a soft button to call to java, in order to use voice recognition.

The context:

C++: btnRecord pressed -> JAVA: startVoiceRecognition -> C++: return;
JAVA: hasResult -> C++: resultHandler -> C++: addchild, removechild, etc.-> crashed randomly.

I figured out it is crashed because of I tried to change the game data when cocos is doing the samething, in the same area.

Ex: when cocos is rendering layerA, JAVA also tried to remove layerA -> crashed.

Does cocos have any solution for this context ?

May be a callback queue which will be processed in the next game loop ?

I think the need to change the Drawing scene when you press some hard button: back key, volume key, or any hardware interrupt event is very necessary.

LongLT
  • 411
  • 6
  • 19

1 Answers1

0

yeah, finally I have solved this sh*t.

JNI callback run in seperated thread. This means when JNI occurs a callback, it cannot blocks cocos main thread.

I was afraid that JNI callback will blocks cocos main thread. I should check this first :(

Ok! To solve this, just use std::mutex and scheduleOnce.

like this:

void MyGame::update(float dt)
{
    jniMutex.unlock();
    // do something
    jniMutex.lock();
}

JNI callback()
{
    jniMutex.lock();
    // scheduleOnce something
    jniMutex.unlock();
}

I want to make sure JNI callback run inside MyGame::update(float), therefore I call unlock() from the begining, and call lock() at the end.

Beside that, we should use scheduleOnce instead of try to modify Node structure directly inside update function.

LongLT
  • 411
  • 6
  • 19