Memory leak when using multithreaded physics

yamashi2
Posts: 8
Joined: Sun Sep 06, 2009 2:33 pm

Memory leak when using multithreaded physics

Post by yamashi2 »

Hello !

I am new to bullet and I thank you for your hard work, it's exactly what I needed !
But I have a problem when using multithreaded physics...
I get very important memory leaks just by initializing the threads.
Here is the code :

Code: Select all

	
mOutstandingTasks = 8;
mCollisionConfiguration = new btDefaultCollisionConfiguration();

	mThreadSupportCollision = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo(
								"collision",
								processCollisionTask,
								createCollisionLocalStoreMemory,
								mOutstandingTasks));

    mDispatcher				= new SpuGatheringCollisionDispatcher(mThreadSupportCollision,mOutstandingTasks,mCollisionConfiguration);

	btVector3 worldAabbMin(-10000,-10000,-10000);
    btVector3 worldAabbMax(10000,10000,10000);

	mBroadphase				= new btAxisSweep3(worldAabbMin,worldAabbMax,mProxies);

	mThreadSupportSolver	= new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo(
								"solver",
								processSolverTask,
								createSolverLocalStoreMemory,
								mOutstandingTasks));
    // The actual physics solver
    mSolver					= new btParallelSequentialImpulseSolver(mThreadSupportSolver,mOutstandingTasks);

    // The world.
    mDynamicsWorld			= new btDiscreteDynamicsWorld(mDispatcher,mBroadphase,mSolver,mCollisionConfiguration);

	mDynamicsWorld->getSolverInfo().m_numIterations = 4;
	mDynamicsWorld->getSolverInfo().m_solverMode = SOLVER_SIMD+SOLVER_USE_WARMSTARTING;

	mDynamicsWorld->getDispatchInfo().m_enableSPU = true;
	mDynamicsWorld->setGravity(btVector3(0,-40,0));
And the clean up code :

Code: Select all

	delete mDynamicsWorld;
    delete mSolver;
	delete mThreadSupportSolver;
	delete mBroadphase;
	delete mDispatcher;
	delete mThreadSupportCollision;
    delete mCollisionConfiguration;
And the memory leak output :

Code: Select all

{253} normal block at 0x0606F548, 161763 bytes long.
 Data: <    H           > CD CD CD CD 48 F5 06 06 CD CD CD CD CD CD CD CD 
{251} normal block at 0x06047AD8, 161763 bytes long.
 Data: <     z          > CD CD CD CD D8 7A 04 06 CD CD CD CD CD CD CD CD 
{249} normal block at 0x06020068, 161763 bytes long.
 Data: <    h           > CD CD CD CD 68 00 02 06 CD CD CD CD CD CD CD CD 
{247} normal block at 0x04F53730, 161763 bytes long.
 Data: <            07  > CD CD CD CD CD CD CD CD CD CD CD CD 30 37 F5 04 
{245} normal block at 0x04F2BCC0, 161763 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD C0 BC F2 04 
{243} normal block at 0x04F04250, 161763 bytes long.
 Data: <            PB  > CD CD CD CD CD CD CD CD CD CD CD CD 50 42 F0 04 
{241} normal block at 0x04EDC7E0, 161763 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD E0 C7 ED 04 
{238} normal block at 0x04EB4FC0, 161763 bytes long.
 Data: <             O  > CD CD CD CD CD CD CD CD CD CD CD CD C0 4F EB 04 
{220} normal block at 0x04EAF7A8, 21408 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{219} normal block at 0x04EAA3C8, 21408 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{217} normal block at 0x04EA4FE8, 21408 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{216} normal block at 0x04E9FC08, 21408 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{214} normal block at 0x04E9A828, 21408 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{212} normal block at 0x04E95448, 21408 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{209} normal block at 0x04E90068, 21408 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{208} normal block at 0x00977E50, 21408 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
As you can see every block is present 8 times which is also the max number of outstanding tasks I am using...
Did I forget something ?

Thanks !
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Memory leak when using multithreaded physics

Post by Erwin Coumans »

Thanks a lot for the report. It might be that some memory is not freed, in particular the local store memory created by createSolverLocalStoreMemory(). We need to keep track of this, and delete it at the end of the program/simulation.

For now, can you try to fix it like this?

Code: Select all


btAlignedObjectArray<void*> sLocalStorePointers;

void* createCollisionLocalStoreMemoryWithDelete()
{
void* localStore = createCollisionLocalStoreMemory();
sLocalStorePointers.push_back(localStore);
return localStore;
}
And pass the createCollisionLocalStoreMemoryWithDelete instead of createCollisionLocalStoreMemory into the construction:

Code: Select all

 mThreadSupportCollision = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo(
                        "collision",
                        processCollisionTask,
                        createCollisionLocalStoreMemoryWithDelete,
                        mOutstandingTasks));
and at the end of the program delete all pointers in sLocalStorePointers:

Code: Select all

for (int i=0;i<sLocalStorePointers.size();i++)
{
delete sLocalStorePointers[i];
}
sLocalStorePointers.clear();
By the way: please note that the btParallelSequentialImpulseSolver will be deprecated in Bullet 2.75. If you are PS3 licensee, you can get an improved constraint solver for SPU from PS3 Devnet. It is fine to continue using the SpuGatheringCollisionDispatcher.

Hope this helps,
Erwin
yamashi2
Posts: 8
Joined: Sun Sep 06, 2009 2:33 pm

Re: Memory leak when using multithreaded physics

Post by yamashi2 »

Thanks a lot, I will try it as soon as I get home.
yamashi2
Posts: 8
Joined: Sun Sep 06, 2009 2:33 pm

Re: Memory leak when using multithreaded physics

Post by yamashi2 »

Hi again ^^

It worked like a charm ! thx
But I also get a memory leak everytime I call the stepSimulation function...
I get a 259 Byte long memory leak each frame...
Any ideas ?

Thanks again !