Hi, I’m trying to destroy a node in Contact2DType.BEGIN_CONTACTcallback but get
You are trying to destroy a object twice or more. error.
I’m also checking for
if (this.node && this.node.isValid) this.node.destroy();
just to be sure, but nothing changes.
If I wait fo the next loop it works as expected
setTimeout(() => {
if (this.node && this.node.isValid) this.node.destroy();
}, 0)
Any advice ?
linrm
October 26, 2022, 2:17am
2
This error can be debugged here to see where it is coming from.
Ronsku
October 26, 2022, 9:41pm
3
I had similar issues over the years with for example destroy() and instantiate(), they are synchronous functions, but isn’t generated or destroyed completely synchronously, so you have to play around with setTimeout() sometimes to ensure you don’t crash your software. This happes mostly on bigger scenes/prefabs and/or slower devices.
A few years back I made an issue about this on Cocos github, my example wasn’t the best, since it is hard to replicate on a fresh small game and the ticket got closed .
opened 11:35AM - 08 Jun 20 UTC
closed 11:14AM - 05 Aug 21 UTC
### Creator version?(版本号)
- All versions up to 2.3.3
### Affected platform?(… 受影响的平台)
- Windows / macOS
### Description
This happens for many things in the game, but as a good and easy way to understand the problem is to explain with cc.destroy().
When you destroy a node with cc.destroy() or destroy all children or similar, the call is synchronous, while the actual action is asynchronous. This causes a lot of weird scenarios where you might be able to do some action on the node after the destroy() has been called.
This also happens with cc.NodePool().put() which is a synchronous call to the engine but works asynchronously.
I've seen that there will proper async handling in CC 2.5, I hope things like these are though of, cause it causes a lot of strange behavior when you don't take them into account and you have to run code like
```
cc.destroy()
setTimeout(() => wait for the current callstack to complete and hope that destroy is ready in the next callstack);
/** if cc.destroy() would return a promise, you could easily just:
* await cc.destroy()
* or cc.destroy.then(...)
* and be sure it is deleted.
**/
```
### How to reproduce?(如何重现)
- Create a node with a button.
- Create a script with one function that calls cc.destroy(this.node) and apply it to the button node.
- Link the button click event to the function that runs cc.destroy(this.node) on the same node.
- Run the project and spam-click the button
- Now you check browser developer console and see that you call destroy of null, because you are able to send multiple click events and call multiple times cc.destroy(this.node) on the same node before it is actually destroyed.

thank you, it is not a big issue to move onto the next event loop but what is strange is that I cannot see others having this issue here in the forum. Anyway this thread contains a possible fix and an explanation, it’s fine to me.
Ive experienced the same issue when trying to destroy some nodes on onContactBegin. Let me try using a setTimeOut()