Do I need to cleanup the old shape when using SetShape() on a body? #175
-
I'm still not 100% on ref based objects so not sure if I need to cleanup certain things. Right now I do: const newShape = newShapeSettings.Create().Get();
body.SetShape(newShape); but I could do: const existingShape = body.GetShape();
body.SetShape(newShape);
jolt.destroy(existingShape); I ask this because when I set vectors I have to destroy the vector: //leaves vector hanging in memory
const scaledShape = baseShape.ScaleShape(new jolt.Vec3(1,2,3)).Get();
// with cleanup
const scaleVec = new jolt.Vec3(4,5,6);
const otherScaledShape = baseShape.ScaleShape(scaleVec).Get();
jolt.destroy(scaleVec) Right now it's obvious to destroy the vector because I created it a line above it. However, it's likely I also created that shape too in an earlier call to So I'm just not sure if I need to destroy it or not.... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You would not call destroy. This is automatically handled inside Jolt due to it being a special const existingShape = body.GetShape();
body.SetShape(newShape);
//do not call this jolt.destroy(existingShape);
SetShape does not create a new shape, it just assigns the pointer Example FlowAssuming [Body] and the 2x shapes, A & B.
When you either create [Body] or assign [Body] shape A, shape A gains an AddRef internally:
When you call "SetShape B", it will internally trigger:
No Shape related memory management is needed by your JS code in this flow I had to dig down to see where this Add/Release happened, and it is kind of concealed, but this is a side-effect on the line : The logic is "Internally, if I am assigning a new internal Shape pointer to an existing ConstReference, Release the current reference and AddRef the new one" DetailsJolts internal management will assume ownership and automatic deletion as soon as you assign it to at least one Body for usage. When the last
Regarding your second block, it is best practice to avoid Assuming you are not tracking via |
Beta Was this translation helpful? Give feedback.
You would not call destroy. This is automatically handled inside Jolt due to it being a special
Reference
class, assuming you manage shape-creation and shape-settings correctly. If that Shape was not used elsewhere by another Reference, by the time you runjolt.destroy
Jolt already destroyed it during SetShape and you are double-free-ing the memory.SetShape does not create a new shape, it just assigns the pointer
newShape
to the body. This allows you to make a single shape, such as a giant Me…