Memory leak in Bo2D 2.1

Thanks for your help.
I create/destroy body after the world.step() (not into a b2contactListener)

I will make a short project to reproduce it.

[edit]
Ok I have done a new topic (as it is version 2.1a)
viewtopic.php?f=8&t=6430
This method will cause bug.

As the registers is initialized sequently. For example, there is primary with type: b2Shape.e_polygonShape, and secondary with type: b2Shape.e_circleShape, but there is no opposite one.

Code: Select all


   b2internal function InitializeRegisters() : void{
      m_registers = new Vector.<Vector.<b2ContactRegister> >(b2Shape.e_shapeTypeCount);
      for (var i:int = 0; i < b2Shape.e_shapeTypeCount; i++){
         m_registers[i] = new Vector.<b2ContactRegister>(b2Shape.e_shapeTypeCount);
         for (var j:int = 0; j < b2Shape.e_shapeTypeCount; j++){
            m_registers[i][j] = new b2ContactRegister();
         }
      }
      
      AddType(b2CircleContact.Create, b2CircleContact.Destroy, b2Shape.e_circleShape, b2Shape.e_circleShape);
      AddType(b2PolyAndCircleContact.Create, b2PolyAndCircleContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_circleShape);
      AddType(b2PolygonContact.Create, b2PolygonContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_polygonShape);
      
      AddType(b2EdgeAndCircleContact.Create, b2EdgeAndCircleContact.Destroy, b2Shape.e_edgeShape, b2Shape.e_circleShape);
      AddType(b2PolyAndEdgeContact.Create, b2PolyAndEdgeContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_edgeShape);
   }


liguorien wrote: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;
      }
   }
The memory leaks in Box2d are all about object pooling. When you have mass of objects, box2d will generate lots of object instances (like b2Contact and b2DynamicTreeNode), and even after they are destroyed, they are still there - in the pools, they will be garbage collected only after they have been pulled out the pool. This will cause very high memory consumption and frequent garbage collection.

I found two positions of memory leak in Box2d.

One for the pooling of b2Contacts in b2ContactsFactory. The b2Fixture and b2Contact references is not immediately cleared in b2ContactsFactory.Destroy function. The codes below can clear them correctly:

Code: Select all

public function Destroy(contact:b2Contact) : void{
      if (contact.m_manifold.m_pointCount > 0)
      {
         contact.m_fixtureA.m_body.SetAwake(true);
         contact.m_fixtureB.m_body.SetAwake(true);
      }
      
      var type1:int = contact.m_fixtureA.GetType();
      var type2:int = contact.m_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];
      
      if (true)
      {
         reg.poolCount++;
         contact.m_next = reg.pool;
         reg.pool = contact;
         // Bug fix
         contact.m_fixtureA = null;
         contact.m_fixtureB = null;   
         contact.m_nodeA.contact = null;
         contact.m_nodeA.prev = null;
         contact.m_nodeA.next = null;
         contact.m_nodeA.other = null;
         contact.m_nodeB.contact = null;
         contact.m_nodeB.prev = null;
         contact.m_nodeB.next = null;
         contact.m_nodeB.other = null;
      }
      
      var destroyFcn:Function = reg.destroyFcn;
      destroyFcn(contact, m_allocator);
   }


And there is also memory leak in b2DynamicTree.FreeNode function. the b2DynamicTreeNode will maintain a b2Fixture instance in userData variable. The code below can clear it correctly:

Code: Select all

private function FreeNode(node:b2DynamicTreeNode):void
      {
         node.parent = m_freeList;
         m_freeList = node;
         // Bug fix
         node.userData = null;
      }


These codes are all tested in my game, hope it helps 😉
And I also RECOMMENDED to clear references in b2Fixture.Destroy function:

Code: Select all

b2internal function Destroy():void
   {
      // The proxy must be destroyed before calling this.
      //b2Assert(m_proxyId == b2BroadPhase::e_nullProxy);
      
      // Free the child shape
      m_shape = null;
      // Bug fix
      m_massData = null;
      m_aabb = null;
      m_body = null;
      m_proxy = null;
      m_filter = null;
      m_userData = null;
   }


And also b2Body, like pinfred suggested in http://www.box2d.org/forum/viewtopic.php?f=8&t=6430.

These will reduce the time for garbage collector to find lonely objects.
Sorry, I made mistake here, liguorien is right and the code is working. This problem is indeed a memory leak of box2d 2.1a, not just about object pooling problem.

yeggo wrote:This method will cause bug.

As the registers is initialized sequently. For example, there is primary with type: b2Shape.e_polygonShape, and secondary with type: b2Shape.e_circleShape, but there is no opposite one.

Code: Select all


   b2internal function InitializeRegisters() : void{
      m_registers = new Vector.<Vector.<b2ContactRegister> >(b2Shape.e_shapeTypeCount);
      for (var i:int = 0; i < b2Shape.e_shapeTypeCount; i++){
         m_registers[i] = new Vector.<b2ContactRegister>(b2Shape.e_shapeTypeCount);
         for (var j:int = 0; j < b2Shape.e_shapeTypeCount; j++){
            m_registers[i][j] = new b2ContactRegister();
         }
      }
      
      AddType(b2CircleContact.Create, b2CircleContact.Destroy, b2Shape.e_circleShape, b2Shape.e_circleShape);
      AddType(b2PolyAndCircleContact.Create, b2PolyAndCircleContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_circleShape);
      AddType(b2PolygonContact.Create, b2PolygonContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_polygonShape);
      
      AddType(b2EdgeAndCircleContact.Create, b2EdgeAndCircleContact.Destroy, b2Shape.e_edgeShape, b2Shape.e_circleShape);
      AddType(b2PolyAndEdgeContact.Create, b2PolyAndEdgeContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_edgeShape);
   }


liguorien wrote: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;
      }
   }

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