memory usage of world->getDispatcher()->findAlgorithm()

julian
Posts: 2
Joined: Tue Jun 07, 2011 3:50 pm

memory usage of world->getDispatcher()->findAlgorithm()

Post by julian »

hello all

i've got a very simple function that uses bullet to check whether 2 objects in my simulation are currently intersecting, it looks like this:

Code: Select all

BOOL doesObjectIntersectWithObject(SceneNode * otherObject)
{

   	 btCollisionObject otherCollisionObject = [otherNode collisionObject];
 	[... snip ... setup rotations of both objects..]
    
    	btCollisionAlgorithm* pAlgorithm = collisionWorld->getDispatcher()->findAlgorithm(&otherCollisionObject, &collisionObject);
	btManifoldResult oManifoldResult(&otherCollisionObject, &collisionObject);
	pAlgorithm->processCollision(&otherCollisionObject, &collisionObject, collisionWorld->getDispatchInfo(), &oManifoldResult);
	btPersistentManifold* contactManifold = oManifoldResult.getPersistentManifold();
    
	if (contactManifold == NULL)
	{
		pAlgorithm->~btCollisionAlgorithm();
		return NO;
	}
    
	int numContacts = contactManifold->getNumContacts();
	for (int j = 0; j < numContacts; j++)
	{
		btManifoldPoint& pt = contactManifold->getContactPoint(j);

		if (pt.getDistance() < 0.00f)
		{
			pAlgorithm->~btCollisionAlgorithm();
			return YES;
		}
	}
    
	pAlgorithm->~btCollisionAlgorithm();
	return NO;
}
now the problem is, every time collisionWorld->getDispatcher()->findAlgorithm() is called, memory usage increases. it is not really leaked since it is released at the end of the simulation when the collision objects are freed, but since the memory usage increases by half a megabyte per second this is still highly undesired. shouldn't the memory that findAlgorithm() claims be released when

pAlgorithm->~btCollisionAlgorithm();

is called? any ideas? i'm now caching the algorithms as a workaround, but i am sure there must be some better way.

btw this is occurring in 2.77 2.78 and trunk.
Kanttori
Posts: 38
Joined: Thu Jul 08, 2010 12:52 am

Re: memory usage of world->getDispatcher()->findAlgorithm()

Post by Kanttori »

Hi!

Haven't used explicit destructor calls (first time saw them in bullet), but I'd assume you'd also need to call btCollisionDispatcher::freeCollisionAlgorithm after the destructor to also free the memory. Basically the destructor does the cleanup, but the memory remains 'allocated' and the pooling system can't reuse it.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: memory usage of world->getDispatcher()->findAlgorithm()

Post by Erwin Coumans »

Indeed. You need to use collisionWorld->getDispatcher()->freeCollisionAlgorithm(algorithm); after calling the destructor.

See this implementation:

Code: Select all


void	btCollisionWorld::contactPairTest(btCollisionObject* colObjA, btCollisionObject* colObjB, ContactResultCallback& resultCallback)
{
	btCollisionAlgorithm* algorithm = getDispatcher()->findAlgorithm(colObjA,colObjB);
	if (algorithm)
	{
		btBridgedManifoldResult contactPointResult(colObjA,colObjB, resultCallback);
		//discrete collision detection query
		algorithm->processCollision(colObjA,colObjB, getDispatchInfo(),&contactPointResult);

		algorithm->~btCollisionAlgorithm();
		getDispatcher()->freeCollisionAlgorithm(algorithm);
	}

}
Hope this helps,
Erwin
julian
Posts: 2
Joined: Tue Jun 07, 2011 3:50 pm

Re: memory usage of world->getDispatcher()->findAlgorithm()

Post by julian »

thanks alot to both of you. since i already have the caching code now ... is caching the returned collision algorithms still a valid optimization? it seems to work fine except after when i have switched to or from fullscreen (new gl context), where it leads to a crash...

EDIT: removing cached algorithms on GLContext recreation removes that problem too. thanks again.