Memory Leak, box2d 2.1a flash. B2CircleShape body

Hi, I've come across a Memory Leak in box2d 2.1a flash version. Was wondering if anyone is still supporting it, or knows their way around the code enough to recomend a fix for it. or could let me know if there is a more up to date version i should be using ?.

The leak occurs when the following conditions occur.

A B2PolygonShape is created and added to the world.
A B2CircleShape is created and added to the world.
These two body's colide.
Both shapes are destroyed from the world using world.DestroyBody(body)

The specifics of the leak are that after this behaviour has occured, the body of the B2CircleShape remains in memory and isn't garbage collected by flash (along with a multitude of additional box2d classes according to the profiler, one of which i expect is the actual cause of the problem if it is referencing the Circle's body).

This only occurs to the B2CircleShape's body, and only once it has collided with a B2PolygonShape whilst it was in the world. If the B2CircleShape is destroyed before it colides with any other body, or after it has collided with other B2CircleShapes, then it's body is garbage collected correctly and memory is freed. If the B2PolygonShape which the B2CircleShape collided with is destroyed, the polygon is garbage collected correctly, but this has no effect of the garbage collection of the B2CircleShape's body which remains in memory.

If anyone knows how to rectify this behaviour, or can explain how collisions between polygon's and circles are handled within this version of Box2d it would be a great help.
Are you trying to destroy the bodies from within a contact listener? Or more specifically, during a world step? If so then it should not be possible to destroy the bodies because they are still needed by Box2D until it has finished processing the time step. In the C++ version attempting this will cause an assert to let you know that you can't destroy them - perhaps the Flash version silently ignores this, I don't know.
irresistible force wrote:Are you trying to destroy the bodies from within a contact listener? Or more specifically, during a world step? If so then it should not be possible to destroy the bodies because they are still needed by Box2D until it has finished processing the time step. In the C++ version attempting this will cause an assert to let you know that you can't destroy them - perhaps the Flash version silently ignores this, I don't know.


No I'm not. I'm destroying body's outside the world step.

This problem is only affecting B2CircleShape body's which have collided with B2PolygonShape body's whilst they were within the world.

it isn't affecting (which i expect it would if it were in my code) the following body's in the following conditions

a) B2PolygonShape body's which have colided with nothing.
b) B2PolygonShape body's which have colided with B2PolygonShape body's
c) B2PolygonShape body's which have colided with B2CircleShape body's
d) B2CircleShape body's which have colided with nothing.
e) B2CircleShape body's which have colided with B2CircleShape body's

all of which delete correctly, their body's and other box2d classes are removed and freed from memory by flash's "garbage colection".
I've created a simulation in AS3 to demonstrate the memory leak the simplest way i feel i can.

The code below is the entire simulation, It:
a) Creates the World
b) Sets up DebugDraw.
c) Creates either a static Polygon or Circle (in staticshape() , see comments to see which lines to uncomment to switch between the two shape types)
d) Creates either a dynamic Polygon or Circle (in fallingshape() , see comments again to change to the other shape type)
e) steps time until the fallingshape has collided with the staticshape, and bounced down out of the way.
f) It then deletes the fallingshape from the world, and recreates another fallingshape back at the top to begin the loop again

When the staticshape is set to be a PolygonShape, and the fallingshape is set to be a CircleShape the memory leak occurs.

Every other combination of these two shapes hitting each other the memory leak does not occur !

Code: Select all

package
{
   import Box2D.Collision.Shapes.b2CircleShape;
   import Box2D.Collision.Shapes.b2PolygonShape;
   import Box2D.Common.Math.b2Vec2;
   import Box2D.Dynamics.b2Body;
   import Box2D.Dynamics.b2BodyDef;
   import Box2D.Dynamics.b2DebugDraw;
   import Box2D.Dynamics.b2Fixture;
   import Box2D.Dynamics.b2FixtureDef;
   import Box2D.Dynamics.b2World;
   import flash.display.Sprite;
   import flash.events.Event;
   
   public class Main extends Sprite
   {
      public const worldRatio:int = 30;
      private var debugDraw:b2DebugDraw;
      private var debugSprite:Sprite;
      private var world:b2World;
      private var fallingbody:b2Body;

      public function Main():void
      {
         if (stage) init();
         else addEventListener(Event.ADDED_TO_STAGE, init);
      }
      
      private function init(e:Event = null):void
      {
         removeEventListener(Event.ADDED_TO_STAGE, init);
         //setup simulation
          world = new b2World(new b2Vec2(0,10.0), true);
         definedebugdraw();
         staticshape();
         fallingshape();
         addEventListener(Event.ENTER_FRAME, update);
      }
      
      private function update(e:Event) :void {
         if (fallingbody.GetPosition().y >= 10) {
            world.DestroyBody(fallingbody);
            fallingshape();
         }
         stepworld();
      }

      private function stepworld() :void {
         world.Step(1/30, 3, 5);
         world.ClearForces();
         world.DrawDebugData();
      }

      private function definedebugdraw() :void {
         debugDraw = new b2DebugDraw();
         debugSprite = new Sprite();
         addChild(debugSprite);
              debugDraw.SetSprite(debugSprite);
            debugDraw.SetDrawScale(worldRatio);
            debugDraw.SetFillAlpha(0.5);
            debugDraw.SetLineThickness(2);
            debugDraw.SetAlpha(1);
            debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
            world.SetDebugDraw(debugDraw);
       }

      private function fallingshape() :void {
         
            var bodyDef:b2BodyDef = new b2BodyDef();
            bodyDef.type = b2Body.b2_dynamicBody;
         bodyDef.position.Set(8, 5);

         //**** create either a falling b2PolygonShape or a b2CircleShape ****
         //**the following 2 lines create a Polygon and set as a box **
         //var myShape:b2PolygonShape = new b2PolygonShape();
            //myShape.SetAsBox(1, 1);
         //** the following 1 line creates a Circle **
         var myShape:b2CircleShape = new b2CircleShape(1);
         
         var fixtureDef:b2FixtureDef = new b2FixtureDef();
            fixtureDef.shape = myShape;
            fixtureDef.density = 1;
            fixtureDef.restitution = 1;
            fallingbody = world.CreateBody(bodyDef);
            var fixture:b2Fixture =fallingbody.CreateFixture(fixtureDef);
       }
      
      private function staticshape() :void {
         
            var bodyDef:b2BodyDef = new b2BodyDef();
            bodyDef.type = b2Body.b2_staticBody;
         bodyDef.position.Set(5, 10);
         bodyDef.angle = 1;
         
         //**** create either a static  b2PolygonShape or a b2CircleShape ****
         //** the following 2 lines create a Polygon and set as a box **
         var statShape:b2PolygonShape = new b2PolygonShape();
            statShape.SetAsBox(2, 2);
         //** the following 1 line creates a Circle **
         //var statShape:b2CircleShape = new b2CircleShape(2.5);
         
         var fixtureDef:b2FixtureDef = new b2FixtureDef();
            fixtureDef.shape = statShape;
            fixtureDef.density = 1.0;
            fixtureDef.friction = 0.7;
            fixtureDef.restitution = 0;
            var body:b2Body = world.CreateBody(bodyDef);
         var fixture:b2Fixture = body.CreateFixture(fixtureDef);
      }
   }
}
Penry
I got the same problem. The only solution i see is to use polygons instead of circles.
Do you have any idea how to fix this leak?
Have you ever tried fbox2d: http://code.google.com/p/fbox2d/
I didn't manage to find a fix, so i switched this project over to Alchemy Box2DAS -

(and now i've found that version breaks when encrypting)
This is error in b2ContactFactory


fixed: http://flashgameblogs.ru/blog/box2d/957.html 😁
Glad to know there's a fix - will there be a Box2d 2.1b for flash ?

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