m_userPersistentData and Contact destroyed callback leaks

jamoflaw
Posts: 7
Joined: Sun Jun 20, 2010 7:19 pm

m_userPersistentData and Contact destroyed callback leaks

Post by jamoflaw »

http://www.bulletphysics.org/Bullet/php ... f=9&t=2243

I think I am getting the same issue shown above, the userdata memory in the callback gets leaked due to the contact destroyed callback never being called :(

Has this bug been fixed?
James
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: m_userPersistentData and Contact destroyed callback leak

Post by Erwin Coumans »

I just tried reproducing it, but the contact destroyed callback is just being called.

Can you provide a simple (modified) Bullet demo that reproduces the problem?
Thanks,
Erwin
jamoflaw
Posts: 7
Joined: Sun Jun 20, 2010 7:19 pm

Re: m_userPersistentData and Contact destroyed callback leak

Post by jamoflaw »

In my application I am getting the contact destroyed called sometimes but not every time meaning after the app has quit there are sometimes a few contact points which havent been destroyed.

If you put counters in each then I was finding that destroyed isn't called for every create.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: m_userPersistentData and Contact destroyed callback leak

Post by Erwin Coumans »

Please create and share a reproduction sample.

thanks,
Erwin
jamoflaw
Posts: 7
Joined: Sun Jun 20, 2010 7:19 pm

Re: m_userPersistentData and Contact destroyed callback leak

Post by jamoflaw »

I am getting some odd results with the bullet demos :? have converted the basic demo to have contact callbacks and I am never getting the destroyed callback now think i've done something wrong!! lol

Code: Select all

/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, 
including commercial applications, and to alter it and redistribute it freely, 
subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

//#define TEST_SERIALIZATION 1

#include <sstream>

///create 125 (5x5x5) dynamic object
#define ARRAY_SIZE_X 5
#define ARRAY_SIZE_Y 5
#define ARRAY_SIZE_Z 5

//maximum number of objects (and allow user to shoot additional boxes)
#define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)

///scaling of the objects (0.1 = 20 centimeter boxes )
#define SCALING 1.
#define START_POS_X -5
#define START_POS_Y -5
#define START_POS_Z -3

#include "BasicDemo.h"
#include "GlutStuff.h"
///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files.
#include "btBulletDynamicsCommon.h"
#ifdef TEST_SERIALIZATION
#include "LinearMath/btSerializer.h"
#endif //TEST_SERIALIZATION

#include <stdio.h> //printf debugging

// callback counters
static int contactAddedHits = 0;
static int contactDestroyedHits = 0;

bool PhysicsContactAdded_Callback(btManifoldPoint& cp,	const btCollisionObject* colObj0,int partId0,int index0,const btCollisionObject* colObj1,int partId1,int index1);
bool PhysicsContactDestroyed_Callback(void* userPersistentData);

void BasicDemo::clientMoveAndDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

	//simple dynamics world doesn't handle fixed-time-stepping
	float ms = getDeltaTimeMicroseconds();
	
	///step the simulation
	if (m_dynamicsWorld)
	{
		m_dynamicsWorld->stepSimulation(ms / 1000000.f);
		//optional but useful: debug drawing
		m_dynamicsWorld->debugDrawWorld();
	}
		
	renderme(); 

	glFlush();

	swapBuffers();

}



void BasicDemo::displayCallback(void) {

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
	
	renderme();

	//optional but useful: debug drawing to detect problems
	if (m_dynamicsWorld)
		m_dynamicsWorld->debugDrawWorld();

	glFlush();
	swapBuffers();

	

}

// This method is a callback when the world has experienced a physics tick
void PhysicsTick_Callback(btDynamicsWorld *world, btScalar timeStep) 
{
	// EMPTY	
	std::ostringstream s;

	s << "Contact Added Hits:" << contactAddedHits << "\n" << "Contact Destroyed Hits:" << contactDestroyedHits << "\n"; 
	OutputDebugString(s.str().c_str());
}



void	BasicDemo::initPhysics()
{
	setTexturing(true);
	setShadows(true);

	setCameraDistance(btScalar(SCALING*50.));

	///collision configuration contains default setup for memory, collision setup
	m_collisionConfiguration = new btDefaultCollisionConfiguration();
	//m_collisionConfiguration->setConvexConvexMultipointIterations();

	///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
	m_dispatcher = new	btCollisionDispatcher(m_collisionConfiguration);

	m_broadphase = new btDbvtBroadphase();

	///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
	btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
	m_solver = sol;

	m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);

	m_dynamicsWorld->setGravity(btVector3(0,-10,0));

	// Set the physics tick callback for collision detection
	m_dynamicsWorld->setInternalTickCallback(PhysicsTick_Callback, this);

	// Create teh callbacks
	gContactAddedCallback = &PhysicsContactAdded_Callback;
	gContactDestroyedCallback = &PhysicsContactDestroyed_Callback;


	///create a few basic rigid bodies
	btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
//	btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
	
	m_collisionShapes.push_back(groundShape);

	btTransform groundTransform;
	groundTransform.setIdentity();
	groundTransform.setOrigin(btVector3(0,-50,0));

	//We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
	{
		btScalar mass(0.);

		//rigidbody is dynamic if and only if mass is non zero, otherwise static
		bool isDynamic = (mass != 0.f);

		btVector3 localInertia(0,0,0);
		if (isDynamic)
			groundShape->calculateLocalInertia(mass,localInertia);

		//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
		btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
		btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
		btRigidBody* body = new btRigidBody(rbInfo);

		body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);

		//add the body to the dynamics world
		m_dynamicsWorld->addRigidBody(body);
	}


	{
		//create a few dynamic rigidbodies
		// Re-using the same collision is better for memory usage and performance

		btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
		//btCollisionShape* colShape = new btSphereShape(btScalar(1.));
		m_collisionShapes.push_back(colShape);

		/// Create Dynamic Objects
		btTransform startTransform;
		startTransform.setIdentity();

		btScalar	mass(1.f);

		//rigidbody is dynamic if and only if mass is non zero, otherwise static
		bool isDynamic = (mass != 0.f);

		btVector3 localInertia(0,0,0);
		if (isDynamic)
			colShape->calculateLocalInertia(mass,localInertia);

		float start_x = START_POS_X - ARRAY_SIZE_X/2;
		float start_y = START_POS_Y;
		float start_z = START_POS_Z - ARRAY_SIZE_Z/2;

		for (int k=0;k<ARRAY_SIZE_Y;k++)
		{
			for (int i=0;i<ARRAY_SIZE_X;i++)
			{
				for(int j = 0;j<ARRAY_SIZE_Z;j++)
				{
					startTransform.setOrigin(SCALING*btVector3(
										btScalar(2.0*i + start_x),
										btScalar(20+2.0*k + start_y),
										btScalar(2.0*j + start_z)));

			
					//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
					btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
					btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
					btRigidBody* body = new btRigidBody(rbInfo);
					
					body->setActivationState(ISLAND_SLEEPING);

					body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);

					m_dynamicsWorld->addRigidBody(body);
					body->setActivationState(ISLAND_SLEEPING);
				}
			}
		}
	}


	clientResetScene();


#ifdef TEST_SERIALIZATION
	//test serializing this 

	int maxSerializeBufferSize = 1024*1024*5;

	btDefaultSerializer*	serializer = new btDefaultSerializer(maxSerializeBufferSize);
	m_dynamicsWorld->serialize(serializer);
	
	FILE* f2 = fopen("testFile.bullet","wb");
	fwrite(serializer->m_buffer,serializer->m_currentSize,1,f2);
	fclose(f2);
#endif

#if 0
	bParse::btBulletFile* bulletFile2 = new bParse::btBulletFile("testFile.bullet");
	bool ok = (bulletFile2->getFlags()& bParse::FD_OK)!=0;
	bool verboseDumpAllTypes = true;
	if (ok)
		bulletFile2->parse(verboseDumpAllTypes);
	
	if (verboseDumpAllTypes)
	{
		bulletFile2->dumpChunks(bulletFile2->getFileDNA());
	}
#endif //TEST_SERIALIZATION

}
	

void	BasicDemo::exitPhysics()
{

	//cleanup in the reverse order of creation/initialization

	//remove the rigidbodies from the dynamics world and delete them
	int i;
	for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
	{
		btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
		btRigidBody* body = btRigidBody::upcast(obj);
		if (body && body->getMotionState())
		{
			delete body->getMotionState();
		}
		m_dynamicsWorld->removeCollisionObject( obj );
		delete obj;
	}

	//delete collision shapes
	for (int j=0;j<m_collisionShapes.size();j++)
	{
		btCollisionShape* shape = m_collisionShapes[j];
		delete shape;
	}

	delete m_dynamicsWorld;
	
	delete m_solver;
	
	delete m_broadphase;
	
	delete m_dispatcher;

	delete m_collisionConfiguration;


	
}

// Contact Added callback
bool PhysicsContactAdded_Callback(btManifoldPoint& cp,	const btCollisionObject* colObj0, int partId0, int index0, const btCollisionObject* colObj1, int partId1, int index1)
{
	// A Contact point has been added
	contactAddedHits ++;

	return true;
}

// Contact Removed callback
bool PhysicsContactDestroyed_Callback( void* userPersistentData )
{
	
	contactDestroyedHits ++;

	return true;
}

This is basicdemo.cpp. Let me know if you see anything obvious, I seem to be getting some odd results though.

Thanks
James

Edit: its a MSVC++ specific project as it is using the OutputDebugString to output the number of times the contact removed and added are called.
jamoflaw
Posts: 7
Joined: Sun Jun 20, 2010 7:19 pm

Re: m_userPersistentData and Contact destroyed callback leak

Post by jamoflaw »

I managed to get some of the user persistent data to be deleted.

Below is my code:

Am I using it in the correct way? On average I am getting around 300 hits in teh first callback (create) and 280 in the second (contact destroyed). Should there be a create and destroy for each?

Code: Select all


// User persistent storage for btManifoldPoint
struct ContactPair
{
	ContactPair(GEntity* _object0,GEntity* _object1):object0(_object0),object1(_object1){};
	GEntity* object0;
	GEntity* object1;
};

// Contact Added callback
bool PhysicsContactAdded_Callback(btManifoldPoint& cp,	const btCollisionObject* colObj0, int partId0, int index0, const btCollisionObject* colObj1, int partId1, int index1)
{
	// A Contact point has been added

	GEntity* object0 = static_cast<GEntity*>(colObj0->getUserPointer());
	GEntity* object1 = static_cast<GEntity*>(colObj1->getUserPointer());

	object0->OnCollide(object1);
	object1->OnCollide(object0);

	// Store contact objects for destroyed callback
	cp.m_userPersistentData = new ContactPair(object0,object1);
	static int hits = 0;
	hits ++;

	return true;
}

// Contact Removed callback
bool PhysicsContactDestroyed_Callback( void* userPersistentData )
{
	ContactPair* contacts= static_cast<ContactPair*>(userPersistentData);
	contacts->object0->OnSeperate(contacts->object1);
	contacts->object1->OnSeperate(contacts->object0);
	delete contacts;
	userPersistentData = 0;

	static int hits2 = 0;
	hits2 ++;

	return true;
}
Blacky11
Posts: 3
Joined: Wed Jul 27, 2011 12:17 pm

Re: m_userPersistentData and Contact destroyed callback leak

Post by Blacky11 »

Why cialis nobody has answered??? :?:
kulebril
Posts: 10
Joined: Wed Jan 16, 2013 1:36 pm

Re: m_userPersistentData and Contact destroyed callback leak

Post by kulebril »

i have the same problem, what do we do?
jamoflaw
Posts: 7
Joined: Sun Jun 20, 2010 7:19 pm

Re: m_userPersistentData and Contact destroyed callback leak

Post by jamoflaw »

I have struggled to do anything here, I unfortunately got a new job and it has taken a back seat!

Mem is def still leaking in the program though. I haven't tried the latest version is this still the same?