Using TriMesh - Memory Leak

Fred_FS
Posts: 10
Joined: Sun Sep 27, 2009 9:56 am

Using TriMesh - Memory Leak

Post by Fred_FS »

Hi there!
I want to use TriMeshs in my game, but if I use them, I will get a strange Leak. Seems that something is not deleted correctly, but with any other shapes it worked fine.
So here is some code.

Code: Select all

	btVector3& inertia_new = btVector3(0, 0, 0);
	entity_ = game_env_.smgr->createEntity( id_ + "entity", mesh_filename_ );
	scene_node_ = game_env_.smgr->getRootSceneNode()->createChildSceneNode( position_, rotation_ );
	scene_node_->attachObject( entity_ );
	entity_->setCastShadows(true);

	BtOgre::StaticMeshToShapeConverter converter( entity_ );
	
	if( shape_type == ST_BOX )
		shape_ = converter.createBox();
	else if( shape_type == ST_SPHERE )
		shape_ = converter.createSphere();
	else if( shape_type == ST_CYLINDER )
		shape_ = converter.createCylinder();
	else if( shape_type == ST_CONVEX )
		shape_ = converter.createConvex();
	else if( shape_type == ST_TRIMESH )
		shape_ = converter.createTrimesh();

	if(state == 0)
		state_ = new BtOgre::RigidBodyState(scene_node_, btTransform( BtOgre::Convert::toBullet( rotation_ ), BtOgre::Convert::toBullet( position_ + Ogre::Vector3(0, 0, 0) ) ));
	else 
		state_ = state;

	if(mass < 0 )
		mass = mass_;
	else
		mass_ = mass;

	if(inertia.x < 0 )
	{
		if( mass > 0)
		{
			shape_->calculateLocalInertia(mass, inertia_new);
			inertia_ = inertia_new;
		}
		else
		{
			inertia_ = btVector3(0,0,0);
		}
	}
	else
	{
		inertia_ = BtOgre::Convert::toBullet(inertia);
	}

	body_ = new btRigidBody( mass_, state_, shape_, inertia_ );
	
	level_.get_physics_world()->addRigidBody( body_ );

	// Wenn auch äußerst unschön -> Dem Physikobjekt wird ein String als Userobjekt übergeben, um das  Physikobjekt an das Objekt zu binden
	body_->setUserPointer( this );
	body_->setRestitution( 0.3 );
The function to convert my mesh looks like this:

Code: Select all

assert(mVertexCount && (mIndexCount >= 6) && 
			("Mesh must have some vertices and at least 6 indices (2 triangles)"));

		unsigned int numFaces = mIndexCount / 3;

		btTriangleMesh *trimesh = new btTriangleMesh();
		unsigned int *indices = mIndexBuffer;
		Vector3 *vertices = mVertexBuffer;

		btVector3    vertexPos[3];
		for (unsigned int n = 0; n < numFaces; ++n)
		{
			{
				const Vector3 &vec = vertices[*indices];
				vertexPos[0][0] = vec.x;
				vertexPos[0][1] = vec.y;
				vertexPos[0][2] = vec.z;
			}
			{
				const Vector3 &vec = vertices[*(indices + 1)];
				vertexPos[1][0] = vec.x;
				vertexPos[1][1] = vec.y;
				vertexPos[1][2] = vec.z;
			}
			{
				const Vector3 &vec = vertices[*(indices + 2)];
				vertexPos[2][0] = vec.x;
				vertexPos[2][1] = vec.y;
				vertexPos[2][2] = vec.z;
			}

			indices += 3;

			trimesh->addTriangle(vertexPos[0], vertexPos[1], vertexPos[2]);
		}

		const bool useQuantizedAABB = true;
		btBvhTriangleMeshShape *shape = new btBvhTriangleMeshShape(trimesh, useQuantizedAABB);

		return shape;
And my deleting code is:

Code: Select all

	if(body_)
	{
		body_->forceActivationState( DISABLE_SIMULATION );
		level_.get_physics_world()->removeRigidBody( body_ );
	}
	delete body_;
	delete state_;
	delete shape_;
I am currently using version 2.75.
I hope you can help me and tell me what I forgot to do.

Thanks in advance!
Fred_FS
Fred_FS
Posts: 10
Joined: Sun Sep 27, 2009 9:56 am

Re: Using TriMesh - Memory Leak

Post by Fred_FS »

Hello!
I solved my problem with adding:

Code: Select all

btTriangleMeshShape::~btTriangleMeshShape()
{
	delete m_meshInterface;
}
in btTriangleMeshShap.cpp.
(void*)
Posts: 6
Joined: Tue Sep 15, 2009 2:32 pm

Re: Using TriMesh - Memory Leak

Post by (void*) »

It is an architectural choice, but I would argue that since the caller allocated the memory, then the caller should release the memory.

I just derived from the btBvhTriangleMeshShape then placed the new/delete of the mesh interface in the derived class. Hopefully making future integrations with bullet easier.