Settimeout cause memory leak

hi, every body,i have a code phrase here:

function foo(){
      function createnode(parentnode){
      let node =  new ccnode();
      parentnode.add(node);
}

settimeout(function(){
foo()

}.bind(this), 100);
}

dose this code phrase cause memory leak? I’ll clean the parent node at sometime with parentnode.destroyAllchildren(), but when my application run on ios device with Xcode instrument, which told me lots of memory leak, althrough every leak is about 197 bytes less, but lots of them

if this code is part of a component, then add an timeoutID member to it:

class MyComponent extends cc.Component {
  private timeoutID: number = NaN;
}

then get the id of the timer:

this.timeoutID = setTimeout(/* your params go here */);

then in the onDestroy method of your component:

protected onDestroy() {
  if (!isNan(this.timeoutID)) {
    clearTimeout(this.timeoutID);
  }
}

hopefully it will cleanup correctly.

Yes, buddy, thanks a lot, I know this way to release the function though I don’t put the release code in this phrase, my question is , in this recursive way, dose this node can be destroyed, cause Xcode instrument said that my application have lots of memory leak. I’m not quiet specific where about

i supposed you’ve already tested, but if you remove the setTimeout/clearTimeout, do you still have these leaks?

This is because you are creating an anonymous function and it will be kept in memory.
So, it’s better to create it as a method of an object or extend an existing class thru prototype.

A good read: https://www.lambdatest.com/blog/eradicating-memory-leaks-in-javascript/