cocos2d / cocos2d-html5

Cocos2d for Web Browsers. Built using JavaScript.
https://www.cocos.com
3.07k stars 902 forks source link

[LEAK] CCActionManager is leaking the targets of its cc.HashElements #774

Closed BonsaiDen closed 11 years ago

BonsaiDen commented 11 years ago

This happens when a Node calls removeAllChildren on itself.

Targets in the action manager are not cleaned up correctly, resulting in a rather extreme memory leak.

We were able to to go from 20 mb to 80mb of memory with just a few reloads of our game board.

One would expect that calling removeAllChildren correctly cleans up the internal state.

Right now we're using a rather ugly workaround by keeping track of the leaked Hashes and clearing them after the removeAllChildren call. See: https://github.com/BonsaiDen/cocos2d-html5/blob/b20849977dce0ac73460066772bef564fb69ad2f/cocos2d/actions/CCActionManager.js

dingpinglv commented 11 years ago

Did you set cleanup flag to false when called removeAllChildren ? like this code: node.removeAllChildren(false);

BonsaiDen commented 11 years ago

No, the code does not pass any parameters. Should we explicitly pass true in this case?

dingpinglv commented 11 years ago

It shouldn't pass true to removeAllChildren, the Cleanup Flag default value is true.

I reviewed code of actionManager.js and CCNode.js very careful, but I can't found which code cause momery leak.

The call stack of removeAllChildren function: node.removeAllChildren ->node.cleanup() ->node.stopAllActions() ->node.getActionManager().removeAllActionsFromTarget(node)

At removeAllActionsFromTarget function of ActionManager: we use _searchElementByTarget to search the element, and use _deleteHashElement to delete the element. then remove this element from the _targets array.

In this time, no pointer reference to removed node object, this node object is wait for destroy by GC.

dingpinglv commented 11 years ago

Hi Ivo,

I think this issue has been fixed, at https://github.com/cocos2d/cocos2d-html5/commit/a8f0d691c0105a8d015443a4e241b8bca28d792a

Thank you very much.