I’ve noticed a memory leak when removing an entity that as a parent. And there’s an exception when trying to set the Entity’s parent to undefined
Currently in my Cesium app I am grouping entities together by settings their ‘parent’ entity to a dummy entity. When I remove a child entity, it is still exists in the parent’s _children array. This causes a memory leak, since these
removed children are still retained by the parent’s _children array. I have tried to get the child and set it’s parent to undefined, but this caused an exception - look at the code below for better explanation.
What I would expect is when I remove an entity that has a parent, it would automatically remove it from the parent’s _children array, but I can manage doing this by myself when that ‘undefined’ bug is gone.
Code to reproduce:
var viewer = new Cesium.Viewer(‘cesiumContainer’);
// An entity to serve as a parent to many entities
var groupEntity = viewer.entities.getOrCreateEntity(‘myGroup’);
// An example for a child entity
var ent1 = viewer.entities.add({
id : ‘dummy1’,
polyline : {
positions : Cesium.Cartesian3.fromDegreesArray([-75, 35,
-125, 35]),
width : 5,
material : Cesium.Color.RED
}
});
// Set the relation to the group entity
ent1.parent = groupEntity;
// Expected to be 1, and it is 1
console.log('expected to be 1 and it is ', groupEntity._children.length);
viewer.entities.remove(ent1);
// Expect to be 0 but still 1
console.log('expected to be 0 and it is ’ groupEntity._children.length);
// So, now I attempt to fix - and it throws exception
// since you can’t set a parent to null but only to another ent
ent1.parent = undefined;
``
Entity.js line 286 - Code in yellow throws exception, code in pale-orange - not the best perfomance (o[n] search).
parent : {
get : function() {
return this._parent;
},
set : function(value) {
var oldValue = this._parent;
if (oldValue === value) {
return;
}
var wasShowing = this.isShowing;
if (defined(oldValue)) {
var index = oldValue._children.indexOf(this);
oldValue._children.splice(index, 1);
}
this._parent = value;
value._children.push(this);
var isShowing = this.isShowing;
if (wasShowing !== isShowing) {
updateShow(this, this._children, isShowing);
}
this._definitionChanged.raiseEvent(this, 'parent', value, oldValue);
}
},
``
Currently as a workaround I have done this :
var index = ent1.parent._children.indexOf(ent1);
ent1.parent._children.splice(index,1);
viewer.entities.remove(ent1);
``
Anyway, I plan to abandon the Entity API in favor of primitives since I need to use GroundPrimitives. When I’ll do that I’ll manage my ‘entity groups’ as primitive collections