Crash in v3.9 on touch callback when using std::bind

I’m getting a reproducible crash on a touch callback in Cocos2d-x v3.9. The setup is this: I want to use a member function as a callback to a touch event on a button, so I used std::bind to bind the ‘this’ parameter so I could set a member function of my scene as a callback. To reproduce the crash, I do the following:

  • Have a button in the scene with the callback bound
  • Start clicking on the button (touch started, but HOLD down the button)
  • Programatically remove the button from the scene
  • Release the touch

The crash occurs deep inside standard library code, but the last Cocos-related item on the stack in the onEvent() call inside the EventDispatcher. A “BAD ACCESS” exception. The problem is obviously related to the fact that the button doesn’t exist anymore to end the touch on. I’m not sure if bind() itself is the source. I will try to reproduce it without using bind to see if the crash still occurs. If bind() is the problem, is there any good pattern for dealing with this that allows me to keep using a member function?

Just confirmed that the problem isn’t related to std::bind(). I get the same behaviour if I remove the button “out from under” the touch after having started to touch it. Is this a bug that got fixed in v3.10?

My mistake was that I was using Node::removeChild() rather than MenuItem::removeChild(). Switching to the correct call fixed the issue.