Hieran_Del8 wrote:Where can I find information about memory management, and keeping references for bullet?
The rule is simple: if you new something, you must delete it.
Everything that is allocated in bullet is freed by bullet.
In other terms, you own resources you allocate.
That's it. It's in the manual.
Hieran_Del8 wrote:
I noticed that several objects were deleted by accessing the world object, accessing the indexed collision shapes, then deleting associated motion states, and then deleting the rigid bodies. So is there a need to keep a reference of the motion data state, or even the rigid bodies outside of the world object?
Yes, because nobody will call delete on them.
Notice the demos are meant to be compact and easy to understand, not to to show proper management techniques. Motion states need to be kept somewhere as the app must be able to pull out transforms for rendering.
Hieran_Del8 wrote:
I notice that the collision shapes had to be maintained outside of the world object, and I understand there's some memory savings from reusing those created templates.
Yes, that is correct. The shape is shared across all objects using it.
Hieran_Del8 wrote:
Also, sometimes new was called on one type of object (for example, "new btConvex2dShape") and delete was called on the same object, but when the pointer was cast to something else ("btCollisionShape"). I'm guessing that's a valid operation?
I can't quite read this English so I'm guessing what you mean.
Calling delete on a btCollisionShape* or on a btConvex2dShape* is the same thing (for instances of btConvex2dShape) as btConvex2dShape is derived from btCollisionShape and btCollisionShape has a virtual destructor.
Hieran_Del8 wrote:I can't remember from studies with virtually overridden classes, but I was under the impression this operation caused a memory leak.
Yes, it could cause a memory leak and will potentially do so every time the dtor is not virtual.
Personally I would have made dtors virtual by default but there were quite some good reason to not do so in the past so I cannot really blame C++ for this.
Calling delete is perfectly safe if the dtor is virtual. And properly implemented as the correct function will get called anyway, regardless of the exact pointer type.