I would love some help in this one. I cannot figure out the memory leak issue.
I have around 10 prefabs and the only thing I want to do is to replace one Prefab with another. Destroy the node (not the prefab) that the prefab is in, to release memory.
const newPrefabNode = instantiate(prefab);
this.node.addChild(newPrefabNode);
...
this.node.child[0].destroy(); /* or this.node.destroyAllChildren(); doesn't matter in this case */
const anotherNewPrefabNode = instantiate(anotherPrefab);
this.node.addChild(anotherNewPrefabNode);
I also tried:
somewhere in the code:
this.node.destroyAllChildren(); /* or this.node.children[0].destroy(); */
Then having in the update method:
update(dt) {
if (!cc.isValid(this.node.children[0])) {
const prefabNode = cc.instantiate(prefab);
this.node.addChild(prefabNode);
}
Cause I read that:
Destroy this Object, and release all its own references to other objects. Actual object destruction will delayed until before rendering. After destroy, this CCObject is not usable any more. You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.
http://www.cocos2d-x.org/docs/creator-api/en/classes/Node.html#destroy
I instantiate a lot and in a loop. Wonder why the delete()
doesn’t release any memory since the memory goes up fast and after a while it starts lagging because of memory real issues. delete() doesn’t seem to do anything for me.
Please help <3