Memory Leak from Linking

Post Reply
Geometrian
Posts: 23
Joined: Sat Dec 03, 2011 8:37 pm
Contact:

Memory Leak from Linking

Post by Geometrian » Sat Dec 03, 2011 8:42 pm

Hi,

There appears to be a small memory leak, apparently caused merely by linking. Adding either of the following (they are NEVER run):

Code: Select all

solver = new btSequentialImpulseConstraintSolver();
. . . or

Code: Select all

dynamics = new ::btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collision_configuration);
dynamics->setGravity(btVector3(0,-10,0));
. . . will produce the following CRT output on Windows:

Code: Select all

Detected memory leaks!
Dumping objects ->
{144} normal block at 0x00625088, 32 bytes long.
 Data: <  #     x 5#    > 0E AA 23 00 00 00 00 00 78 CA 35 23 CD CD CD CD 
Object dump complete.
Ideas? Thanks,

Ian

DannyChapman
Posts: 85
Joined: Sun Jan 07, 2007 4:29 pm
Location: Oxford, England
Contact:

Re: Memory Leak from Linking

Post by DannyChapman » Thu Dec 22, 2011 9:43 pm

I'm getting this too with 2.79. If I do:

Code: Select all

  mGliderGame->mCollisionConfiguration = new btDefaultCollisionConfiguration();
  mGliderGame->mDispatcher = new  btCollisionDispatcher(mGliderGame->mCollisionConfiguration);
  mGliderGame->mOverlappingPairCache = new btDbvtBroadphase();
  mGliderGame->mSolver = new btSequentialImpulseConstraintSolver;
  mGliderGame->mDynamicsWorld = new btDiscreteDynamicsWorld(
    mGliderGame->mDispatcher,
    mGliderGame->mOverlappingPairCache,
    mGliderGame->mSolver,
    mGliderGame->mCollisionConfiguration);
then

Code: Select all

  mDynamicsWorld->stepSimulation(1.f/60.f,10);
then clear up

Code: Select all

  // terminate physics
  delete mGliderGame->mDynamicsWorld;
  delete mGliderGame->mSolver;
  delete mGliderGame->mOverlappingPairCache;
  delete mGliderGame->mDispatcher;
  delete mGliderGame->mCollisionConfiguration;
the memory leak checker (I'm using Marmalade) reports a leak. If I miss out the call to stepSimulation then there's no leak reported.

This is without any other Bullet code at all (i.e. no objects yet - though it happens if I create them too).

Digging into it now, but any pointers would be appreciated.

DannyChapman
Posts: 85
Joined: Sun Jan 07, 2007 4:29 pm
Location: Oxford, England
Contact:

Re: Memory Leak from Linking

Post by DannyChapman » Thu Dec 22, 2011 9:46 pm

10 seconds later (well, perhaps 20):

Some hunch made me turn off profiling (#define BT_NO_PROFILE 1 in btQuickprof.h) and that got read of this leak.

Geometrian
Posts: 23
Joined: Sat Dec 03, 2011 8:37 pm
Contact:

Re: Memory Leak from Linking

Post by Geometrian » Sun Feb 05, 2012 7:02 am

Hmmm . . . still getting the leak.

Ripiz
Posts: 47
Joined: Mon Aug 16, 2010 10:43 am

Re: Memory Leak from Linking

Post by Ripiz » Sun Feb 05, 2012 11:56 am

Geometrian, can you post your initialization and destruction?

Code posted by DannyChapman doesn't have any leaks:

Code: Select all

	btDefaultCollisionConfiguration *mCollisionConfiguration = new btDefaultCollisionConfiguration();
	btCollisionDispatcher *mDispatcher = new  btCollisionDispatcher(mCollisionConfiguration);
	btDbvtBroadphase *mOverlappingPairCache = new btDbvtBroadphase();
	btSequentialImpulseConstraintSolver *mSolver = new btSequentialImpulseConstraintSolver;
	btDiscreteDynamicsWorld *mDynamicsWorld = new btDiscreteDynamicsWorld(
		mDispatcher,
		mOverlappingPairCache,
		mSolver,
		mCollisionConfiguration
	);

	mDynamicsWorld->stepSimulation(1.f/60.f,10);

	delete mDynamicsWorld;
	delete mSolver;
	delete mOverlappingPairCache;
	delete mDispatcher;
	delete mCollisionConfiguration;

Geometrian
Posts: 23
Joined: Sat Dec 03, 2011 8:37 pm
Contact:

Re: Memory Leak from Linking

Post by Geometrian » Sun Feb 05, 2012 3:56 pm

There's some bullet code, but it is never ever run. The memory leak appears only when some functionality is linked.
Thanks,
Ian

Ripiz
Posts: 47
Joined: Mon Aug 16, 2010 10:43 am

Re: Memory Leak from Linking

Post by Ripiz » Mon Feb 06, 2012 3:00 pm

So memory leak appears after you add functionality, which is never executed?
In that case, are you sure you destroy all objects you created?

Code: Select all

physics->removeRigidBody(...);
delete RigidBody;
delete CollisionShape;
Are you sure it's not your code causing leak at all?

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

Re: Memory Leak from Linking

Post by Erwin Coumans » Tue Feb 14, 2012 12:58 am

Can you try to use the great free and open source Visual Leak Detector in combination with Visual Studio? It will give you a full call stack where the leaked memory was allocated.
(make sure to statically link against Bullet, and use the DLL version of the C run-time)

It is very easy to use: just add an

Code: Select all

#include <vld.h> 
anywhere in your code base, and link against the right library.

Thanks,
Erwin

Geometrian
Posts: 23
Joined: Sat Dec 03, 2011 8:37 pm
Contact:

Re: Memory Leak from Linking

Post by Geometrian » Tue Feb 14, 2012 1:18 am

When using VLD, no leaks are reported:

Code: Select all

Visual Leak Detector Version 2.2.2 installed.
...
No memory leaks detected.
Visual Leak Detector is now exiting.
CRT produces the output given above. Again:

Code: Select all

Detected memory leaks!
Dumping objects ->
{146} normal block at 0x028952E0, 32 bytes long.
 Data: <_ 3      (      > 5F A8 33 00 00 00 00 00 C9 28 F0 00 CD CD CD CD 
Object dump complete.
Thanks!

Geometrian
Posts: 23
Joined: Sat Dec 03, 2011 8:37 pm
Contact:

Re: Memory Leak from Linking

Post by Geometrian » Sun Apr 07, 2013 7:09 pm

Ripiz wrote:So memory leak appears after you add functionality, which is never executed?
In that case, are you sure you destroy all objects you created?

Code: Select all

physics->removeRigidBody(...);
delete RigidBody;
delete CollisionShape;
Are you sure it's not your code causing leak at all?
Exactly. The code that's causing the leak never runs. My suspicion then is that it's triggering the link inclusion of something in Bullet that's causing a memory leak. With no physics code whatsoever running, the leak still occurs. Commenting these lines out fixes the problem.

I did (re)try to #define BT_NO_PROFILE 1 in LinearMath/btQuickprof.h (line 19), but this time I also rebuilt the libraries. This seems to fix the problem.

Geometrian
Posts: 23
Joined: Sat Dec 03, 2011 8:37 pm
Contact:

Re: Memory Leak from Linking

Post by Geometrian » Tue Nov 26, 2013 9:32 pm

This still occurs in 2.82.

I'm fairly sure at least part of the problem is that the "static btClock gProfileClock;" declared at btQuickprof.cpp:21 allocates memory that is never freed.

Post Reply