I’m facing with a lot of crash when try to addChild, removeChild inside hardware interrupts, or JAVA callback.
My game has a button to call to java, in order to use voice recognition.
The context:
C++: button record pressed -> JAVA: start voice recognition -> C++: return;
JAVA: has result -> C++: result handle function -> 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 this make sense because the need to change the Drawing scene when you press some hard button: back key, volume key, etc… is very necessary
Thank you, Magniffect. Your solution seems very reasonable.
But it has a limitation, we have to add all of events for each scene.
Actually, I tested with scheduleOnce(...) hundred times but it has NOT crashed yet.
I’m not sure it absolutely avoid crashed or just become to more rarely.
I’ll double check how cocos schedule event via scheduleX function.
It would be nice if someone can share the knowledge of schedule event area .
I think your solution is better, it is more short.
The scheduleOnce() calls schedule(), and the schedule() is called between frames. So it must be safe.
I don’t understand why you need remove child immediately ? i think that is bad idea, addChild() and removeChild() is expensive method.
That’s why we call it in init() and release() method. I agree with Magniffect, you can make simple garbageCollector() for remove child, for example giving flag WILL_REMOVED and set visible to false to child that you want to be removed. Then use schedule function (e.g every 5 minutes) you will execute removeChild() to children that have flag WILL_REMOVED.
For schedule event, I think you can implement Observer design pattern and Obejct Pool design pattern. CMIIW
Schedule cannot help
The crash will occurs when cocos and JNI do stuff with schedule at the same time.
I tried and it crashed.
I beleived magniffect’s way is the best solution cocos has @fikry I have to remove because to add all of popup events in each scene init is more expensive
You can imagine what’s happens if we have to handle more than 20 events popup?
Create all of them from init is waste memory
Sorry for my urgly english
Other solution, you can use game update for remove event.
I will use setVisible(false) instead removeChild() because if sometime I need to show popup again so I need not to call addChild().
Good Luck.
@quano1
To be honest, I am surprised that this crashed. Could you provide some code so that I can look into it later and improve my knowledge of the schedule.
It become more rarely to crash. But it’s still crashed.
It’s understandable. At the time cocos’s doing stuff with schedule, suddently JNI tries to insert a new event, and ofcourse, crash occurs.
your solution is the same way cocos handles touching event, hard button. This means it should not be crashed.
Because touching and hard button is hardware interrupt. It’s the same with my context.
Im wrong. dispatchEvent will triggers event immediately.
anyway, thank all of you
p/s: all of bad english talking together, do not need to say sorry