Cocos 2dx 3.17 Crashes in iOS devices

Exception : Bad Access.
Place of Occurance : ScriptingCore class in executeFunctionWithOwner function at the end of ‘do’ block, bRet = JS_CallFunctionValue(cx, obj, funcVal, args, retVal) this particular line is throwing exception.

Stack Trace :
ScriptingCore::executeFunctionWithOwner() is called from ScriptingCore::handleTouchEvent().
ScriptingCore::handleTouchEvent() is called from js_EventListenerTouchOneByOne_create() function of jsb_event_dispatcher_manual.cpp class.

Steps to Reproduce :
Launch a game which has multiple touch action.
Start playing the game.
At some point in the game level app crashes.

This is an unfortunate situation and I used to face it frequently too. It happens only on iOS and not on Android. The reason is associated with garbage collection being triggered at the time of drag and that also garbage collects touch related objects.

Fortunately, cocos2d-x allows you to manually control garbage collection. The solution I came up with is this:

  1. on every onTouchEnded event, set timeout to trigger garbage collection after 1s.
  2. on every onTouchBegan, cancel the timeout to garbage collect

I created a small utility to do this: https://gist.github.com/fleon/79106be6c5ef530d37ff1b0b62be9e69

Let me know if this helps you fix the problem.

1 Like