How to get around the memory leak issue in Box2D for javascript port?

612 Views Asked by At

As I understand now the Box2D version for web is leaking memory, bodies are not deleted, contacts neither. So how do I solve this problem?

See my related question here explaining how is leaking: How to properly delete a box2d body in version: Box2dWeb-2.1.a.3, Box2D_v2.3.1r3? Box2D bug?

1

There are 1 best solutions below

2
On

it is possible that this post described how to patch this issue http://devizgl.blogspot.com/2012/03/box2d21a.html

You have to patch file Box2dWeb-2.1.a.3.js

Step 1 Add method Destroy() to the class b2Body:

b2Body.prototype.Destroy = function () {

  this.m_userData = null;
  this.m_sweep = null;
  this.m_xf = null;
  this.m_linearVelocity = null;
  this.m_force = null;
  this.m_world = null;
  this.m_prev = null;
  this.m_next = null;
  this.m_fixtureList = null;
  this.m_controllerList = null;
  this.m_jointList = null;
  this.m_contactList = null;
}

Step 2 Add code to the end of the method DestroyBody of class b2World:

...
--this.m_bodyCount;
  b.Destroy();
}

Step 3 Add this field to the class b2Contact:

...
this.m_swaped = false;

Step 4 Add code to the method Destroy of class b2ContactFactory:

...
var reg = null;   

  if (contact.m_swaped) {
     reg = this.m_registers[type2][type1];
  }
  else {
     reg = this.m_registers[type1][type2];
  }

  contact.Reset();
...

Step 5 Add code to the method Create of class b2ContactFactory:

...
if (reg.pool) {
     c = reg.pool;
     reg.pool = c.m_next;
     reg.poolCount--;
     // <---------------------
     if (c.m_swaped) {
        c.Reset(fixtureB, fixtureA);
     }
     else {
        c.Reset(fixtureA, fixtureB);
     }
     // <---------------------
     return c;
  }

  var createFcn = reg.createFcn;
  if (createFcn != null) {
     if (reg.primary) {
        c = createFcn(this.m_allocator);
        c.Reset(fixtureA, fixtureB);
        c.m_swaped = false;  // <------------------
        return c;
     }
else {
        c = createFcn(this.m_allocator);
        c.Reset(fixtureB, fixtureA);
        c.m_swaped = true;   // <------------------
        return c;
     }
  }
...