如何解决Box2D中javascript端口的内存泄漏问题?

时间:2013-12-30 13:13:18

标签: box2dweb

据我所知,现在用于网络的Box2D版本正在泄漏内存,不会删除正文,也不会删除联系人。那么我该如何解决这个问题呢?

在此处查看我的相关问题,解释如何泄漏:How to properly delete a box2d body in version: Box2dWeb-2.1.a.3, Box2D_v2.3.1r3? Box2D bug?

1 个答案:

答案 0 :(得分:0)

这篇文章可能描述了如何修补此问题 http://devizgl.blogspot.com/2012/03/box2d21a.html

你必须修补文件Box2dWeb-2.1.a.3.js

第1步 将方法Destroy()添加到类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;
}

第2步 将代码添加到类b2World的方法DestroyBody的末尾:

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

第3步 将此字段添加到类b2Contact:

...
this.m_swaped = false;

第4步 将代码添加到类b2ContactFactory的方法:

...
var reg = null;   

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

  contact.Reset();
...

第5步 将代码添加到方法创建类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;
     }
  }
...