btTriangleMeshShape destructor?

N_K
Posts: 14
Joined: Mon Jun 04, 2012 11:40 pm

btTriangleMeshShape destructor?

Post by N_K »

Hi all!

I'm experimenting with Bullet physics, using Ogre as the graphics engine, and BtOgre as a wrapper. Everything works fine, except that there are some strange memory leaks. With the help of a leak detector, I've found out that the problem is related to TriangleMeshShape. Doing a google search returned this thread, and the sollution described there fixed the problem for me.

I had to modify the originally empty destructor in btTriangleMeshShape.cpp to this:

Code: Select all

btTriangleMeshShape::~btTriangleMeshShape()
{
   delete m_meshInterface;
}
But I gues this isn't supposed to be done this way, otherwise it would have been already added to the original code. So my question is, what is the correct way to do this? And how is this done in the Bullet demos? I did a search through all of the demo code, and there are a few TriangleMeshShapes created, but I found no clean-up procedure for them, and yet there are no memory leaks in any of them.

Thanks in advance.
Misterblue
Posts: 14
Joined: Wed Apr 28, 2010 3:09 pm

Re: btTriangleMeshShape destructor?

Post by Misterblue »

I have noticed the same problem -- when creating a btTriangleMeshShape, you pass in a pointer to a btStridingMeshInterface. The btStridingMeshInterface is not copied so, if you had to dynamically create it, you need to know when the btTriangleMeshShape is deleted to be able to also free the btStridingMeshInterface.

On the other had, if the passed in btStridingMeshInterface was a local structure (say, defined locally in the enclosing application class) you can't just free it as you suggest.

The demos get around this freeing problem by not even using a btTriangleMeshShape.

I have "solved" this problem by remembering the pointer to the btStridingMeshInterface that I created and when I destroy the btTriangleMeshShape I also destroy the striding mesh. Not pretty.
frank28_nfls
Posts: 7
Joined: Wed Jul 18, 2012 4:54 am

Re: btTriangleMeshShape destructor?

Post by frank28_nfls »

I think it's the way how Bullet manages the user newed memory, it chooses flexibility instead of simplicity.

In btBulletWorldImporter, it uses basically the solution that Misterblue uses, store all mesh interface in m_allocatedTriangleIndexArrays, to ensure no memory leaks when clean up.

My solution to this is just to derive from btTriangleMeshShape, named 'MyTriangleMeshShape' for example, and in its destructor, I make sure to delete m_meshInterface myself. I hate also to modify the original bullet file. Thanks to polymorphism in C++.