From f2ddfee76844f1a86f4759fa96a8cbe78eb5408c Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 25 Oct 2017 21:02:54 +0800 Subject: [PATCH] fix potential memory leak when node detached from parent without cleanup --- cocos2d/core/utils/scene-graph-helper.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/cocos2d/core/utils/scene-graph-helper.js b/cocos2d/core/utils/scene-graph-helper.js index 734e6bf183..56ec38a13d 100644 --- a/cocos2d/core/utils/scene-graph-helper.js +++ b/cocos2d/core/utils/scene-graph-helper.js @@ -27,15 +27,23 @@ var SceneGraphUtils = { removeSgNode: function () { - var node = this._sgNode; - if (node) { - var parent = node._parent; + var sgNode = this._sgNode; + if (sgNode) { + var parent = sgNode._parent; if (parent) { - parent.removeChild(node); + parent.removeChild(sgNode); } - node.release(); - if (this._sgNode._entity) { - this._sgNode._entity = null; + else { + // cleanup was skipped when its node was detaching + if (CC_JSB) { + sgNode.cleanup(); + } + else { + sgNode.performRecursive(_ccsg.Node.performType.cleanup); + } + } + if (sgNode._entity) { + sgNode._entity = null; } } },