B2Vec2 Box2D Common.Math Memory Leak

Discuss issues specific to miscellaneous ports of Box2D
dkerr
Posts: 5
Joined: Mon Jun 14, 2010 8:59 pm

B2Vec2 Box2D Common.Math Memory Leak

Postby dkerr » Sun Apr 15, 2012 10:04 am

In Flash Builder Profiler, I'm seeing a severe memory leak B2Vec2 Box2D Common.Math that eventually crashes my air app.

How do I stop this leak? I've seen many posts reporting this same issue, but no answers yet.

I've tried all the Destroy Body approaches, ClearForces(), and even System.gc() to attempt to get rid of the leak. Nothing helps.

I'd appreciate any help very much.

Thanks!
Don


I'm simply adding about 20-40 bodies that when they collide, I use this code

private function onBodiesContact(e:b2ContactEvent):void
{
var b1:UIComponent = e.contact.GetFixtureA().GetBody().GetUserData();
var b2:UIComponent = e.contact.GetFixtureB().GetBody().GetUserData();

var bBody1:b2Body = physicsContainer.getComponentBody(b1);
if(bBody1 !== null){

bBody1.ApplyForce(new b2Vec2(30,-30),bBody1.GetDefinition().position);
}
var bBody2:b2Body = physicsContainer.getComponentBody(b2);
if(bBody2 !== null){

bBody2.ApplyForce(new b2Vec2(-30,30),bBody2.GetDefinition().position);
}



}

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: B2Vec2 Box2D Common.Math Memory Leak

Postby jayther » Sun Apr 15, 2012 10:53 pm

What version of Box2D are you using? Box2D doesn't have a b2ContactEvent, so I assume it's custom made or not BorisTheBrave's Box2D AS3 port (2.1a) (and probably not Alchemy).

Is it possible that the leak is somewhere else? b2Vec2 doesn't have anything that could cause any sort of memory leak (it only stores two Number variables, nothing else). ApplyForce wouldn't cause a memory leak either because it only adds onto the body's internal values for force and torque (it doesn't store the passed parameters). These claims are all based on the Box2D AS3 port 2.1a, so if you're using a different version or something, the claims probably are not as valid.

Also, how fast does the memory leak build up?

dkerr
Posts: 5
Joined: Mon Jun 14, 2010 8:59 pm

Re: B2Vec2 Box2D Common.Math Memory Leak

Postby dkerr » Mon Apr 16, 2012 6:04 am

Thank you for your response.
I'm using https://github.com/codeinvain/Box2DFlex , which I believe slightly modifies the core Box2D for Flex. No forum for it, but I can check with the owner.

I'm very sure the leak is with B2Vec2 Box2D Common.Math, because that is the only object where the memory in growing in the Profiler. See attached screen shot. The leak grows very fast. gets to about 500MB in about 10 min. It keeps growing until it maxes out the PC memory.

I have seen a lot of posts in google about B2Vec2 memory leaks with folks using various versions.

I may have to look into getting away from the Flex version and try the latest pure AS3 version.

Don
Attachments
Screen Shot 2012-04-12 at 6.26.53 PM.png
Screen Shot 2012-04-12 at 6.26.53 PM.png (138.43 KiB) Viewed 6472 times

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: B2Vec2 Box2D Common.Math Memory Leak

Postby jayther » Mon Apr 16, 2012 2:08 pm

Ah, I see, the problem is not b2Vec2 itself. There are just a bunch of b2Vec2 objects that are staying referenced somewhere in your code or the Box2D Flex code.

I'm not seeing anywhere in the ApplyForce function in the b2Body code in the Box2D Flex that would cause any sort of leak (i.e. not keeping a reference to the b2Vec2 objects). If you comment out the code inside your onBodiesContact function, do the b2Vec2 objects still build up? If not, then the leak is being caused somewhere else.

dkerr
Posts: 5
Joined: Mon Jun 14, 2010 8:59 pm

Re: B2Vec2 Box2D Common.Math Memory Leak

Postby dkerr » Tue Apr 17, 2012 9:59 am

Yes, I turned off the eventlistener for body contact and still had the problem.

Leak is in b2Vec2 somehow. Others have seen it like this post
viewtopic.php?f=8&t=6430
http://flashgameblogs.ru/blog/box2d/957.html

So, I don't think it is limited to Box2DFlex's code.

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: B2Vec2 Box2D Common.Math Memory Leak

Postby jayther » Tue Apr 17, 2012 2:53 pm

Box2DFlex is adapted from Box2DFlash, so there's no surprise there.

Again, the problem is NOT in b2Vec2, as it is just instances of b2Vec2 that are staying around, shown in your profiler. b2Vec2 just has two Number values stored, which would not cause a memory leak by themselves. What would cause a memory leak is keeping a reference to the b2Vec2 objects and not clearing the reference (for example, an object (not b2Vec2) that's sticking around that's keeping a reference to the b2Vec2 object) so it is never eligible for garbage collection. Again, the problem is not b2Vec2; it is somewhere else in the code.

But yes, it is known by some that there is a memory leak regarding the b2CircleShape, and the flashgameblogs link you provided has a fix for said memory leak. So if the only problem is the memory leak, the fix is there by replacing two files (b2Contact.as and b2ContactFactory.as) in the Box2D source. There doesn't seem to be any difference between the two original files in Box2D Flex and the same files in Box2DFlash, so it may be safe to just replace them in the Box2DFlex source (famous last words. x] Back them up somewhere just in case).

dkerr
Posts: 5
Joined: Mon Jun 14, 2010 8:59 pm

Re: B2Vec2 Box2D Common.Math Memory Leak

Postby dkerr » Tue Apr 17, 2012 3:13 pm

Thank you. I'll give that a try. Appreciate the detailed explanation.

Romance1984
Posts: 5
Joined: Sat Jul 25, 2015 2:42 am

Re: B2Vec2 Box2D Common.Math Memory Leak

Postby Romance1984 » Wed Sep 14, 2016 2:11 am

Again, the problem is not b2Vec2; it is somewhere else in the code.
royal1688


Return to “Miscellaneous”



Who is online

Users browsing this forum: No registered users and 2 guests