Memory leak in Bo2D 2.1

There is a problem: version 2.1 box2D engine does not clear memory properly. I make a game with gun and many bullets (so many bodies are added and removed every second). I'm testing in flashdevelop and in the profiler I see a bunch of objects that accumulate in memory and are not removed. There are most counted: b2Vec2, b2ContactID, Features, b2ManifoldPoint, Vector.<*>, etc. They accumulate even if I do not handle contact events, so don't keep references to contact objects. I do not know how to clean the memory without editing source code of Box2d.

Also, when I remove body with world.DestroyBody() it is don't reset the userData property. It can be fixed by myself (myBody.SetUserData(null)), but it would be better if engine will do it for me.
I'm not sure about the flash problem but I have not seen any memory leaks in the original C++ version of Box2d.

About the user data pointer in a destroyed body, why do you care what it is? You won't be using the deleted body anymore right?
I don't know C++ and can only told about AS3. May be reason of this bug is different ways of work of garbage collector. But Profiler shows that memory is not freed, and after some time flash runs slowly.
If you don't clean userData in AS3 version, the object that stored here will not be deleted from the memory. In my case this are Movieclips.
Just a random guess, as I don't know flash: do you reference box2D objects in your own classes (like the class pointed by your userdata) ? This could create some cyclic pointing that could make garbage collection fail.

In that case, doing myBody.SetUserData(null) like you said seems a reasonable solution.
Are you doing this test on a Release player, and in the browser, or is it just on the debug player that launches automatically on a test publish?

Resource usage will almost always be dramatically better in a release player (one that will NOT report runtime errors) in the browser. You might want to try testing there if you haven't already.
I have tested in Debug FlashPlayer.
I will try use Release Player but I think it must work good anywhere.
I spent the evening hunting down that memory leak.

I figured out that it was related to the b2Contact pooling in b2ContactFactory. If I comment those lines :

Code: Select all

reg.poolCount++;
contact.m_next = reg.pool;
reg.pool = contact;


... the leak disappears. I haven't (yet) figured out why, but if the b2Contact instance stay in memory, pretty much most of the core objects also stay in memory (b2Body, b2Fixture, b2Manifold, etc...) and never get garbage collected.

I'm still working on a fix.
Ok I think I found why. It's related to the primary/secondary registers thing.

The factory never checks in the secondary register for pulling instances out of the pool.

This is probably not the best way to do it, but the following code fix the issue :


Code: Select all

public function Create(fixtureA:b2Fixture, fixtureB:b2Fixture):b2Contact{
      var type1:int = fixtureA.GetType();
      var type2:int = fixtureB.GetType();
      
      //b2Settings.b2Assert(b2Shape.e_unknownShape < type1 && type1 < b2Shape.e_shapeTypeCount);
      //b2Settings.b2Assert(b2Shape.e_unknownShape < type2 && type2 < b2Shape.e_shapeTypeCount);
      
      var reg:b2ContactRegister = m_registers[type1][type2];
      
      var c:b2Contact;            
      
      if (reg.pool)
      {
         // Pop a contact off the pool
         c = reg.pool;
         reg.pool = c.m_next;
         reg.poolCount--;
         c.Reset(fixtureA, fixtureB);
         return c;
      }
      
      reg = m_registers[type2][type1];
      
      if(reg.pool){
         c = reg.pool;
         reg.pool = c.m_next;
         reg.poolCount--;
         c.Reset(fixtureB, fixtureA);
         return c;
      }
      
      var createFcn:Function = reg.createFcn;
      if (createFcn != null)
      {
         if (reg.primary)
         {
            c = createFcn(m_allocator);
            c.Reset(fixtureB, fixtureA);            
            return c;
          }
         else
         {
            c = createFcn(m_allocator);
            c.Reset(fixtureA, fixtureB);      
            return c;
         }
      }
      else
      {
         return null;
      }
   }
Hi,

Agree with you that there is a memory leak.
I downloaded Box2D2.1a.
I use flexbuilder with profiler to monitor occurances of b2Vec2.

When I make several creation/destruction of body, b2Vec2 instances grow over and over...

I try your patch => same problem.

Is there a way to fix it ? (I can make a sample test project if necessary)

Thanks

Pinfred
In my case it was b2World.IsLocked() function.
In previous version of Box2D engine I Have received an error if IsLocked() returnes true but in 2.1 it's just nothing happens without any error or message. So if you want create or destroy a body you must do it outside the Step function.
May be it will help you.

Original thread: https://box2d.org/forum/viewtopic.php?t=5891