AS3 Port: userData doesn't seem to be released properly.

Version 2.0.2 (as far as I know, that's the latest...right?).

Using Adobes Flex 3 profiler, I noticed that my "Actor" objects didn't seem to get garbage collected like everything else when a level change occurred. I tracked down the problem to body.userData; it seems like the reference it holds is not marked for garbage collection when world.DestroyBody(body) is called.

My workaround was to add the following to the end of the DestroyBody function in b2World:
Code:
b.m_userData = null;
b = null;

And that seems to work fine.

PS: I love Box2D! Keep up the good work guys.
b=null -? Isn't it useless for your problem?

Anyway, it's working in a right way.
Neosano wrote:
b=null -? Isn't it useless for your problem?


Yea, I guess. Originally that was the only thing I did, thinking that userData, being a member of the body, would be properly cleared when the body was set to null. However, it turned out that you have to set userData to null explicitly, regardless of what you do to the body. I just left it in there out of paranoia.

Quote:
Anyway, it's working in a right way.


So, userData remaining uncleared is intended Box2D behavior? Or maybe you're saying "it's working right for you now, so it's problem solved"?

Sorry if I misunderstood.
milo wrote:
So, userData remaining uncleared is intended Box2D behavior? Or maybe you're saying "it's working right for you now, so it's problem solved"?

If I'm understanding your issue correctly, that's just the way GC works, nothing specific to Box2d - once a body is cleared from the world, Box2d will no longer hold references to it, but your own code might, and there's nothing Box2d can do about that. Until all refs to a body are gone, those objects are reachable and thus ineligible for GC.
ewjordan wrote:
If I'm understanding your issue correctly, that's just the way GC works, nothing specific to Box2d - once a body is cleared from the world, Box2d will no longer hold references to it, but your own code might, and there's nothing Box2d can do about that. Until all refs to a body are gone, those objects are reachable and thus ineligible for GC.


Ok, but I ran tests to establish whether its my code or Box2D:

First, I ran the profiler where body.userData was set to reference the relevant "Actor" object. What resulted was a compounding instance count (bad). However, I noticed that there were some objects for which the instance count was as expected from one level change to another - these objects were just decoration, and therefore had no bodies assigned.

This led me to believe that the problem had something to do with the Box2D side of things; more specifically, the body.

Having noticed that the only relationship the body had with the Actor was through the "userData" member, I decided to leave that particular variable unassigned, and ran the profiler again to see what would happen. What resulted was the expected instance count for all objects when levels were changed (good). Leading me to conclude that "userData" is not being properly released when world.DestroyBody(body) is called.

So, that would make this a Box2D problem. Right?
No. Because it might be YOU that is holding a reference to the b2Body, preventing it and the actor from being cleaned up. I'll have a look though, it is possible there are references being held by box2d. It's not as big a problem as you think, as any such mistake would be from Box2D recycling objects, so the problem will sort itself out eventually.

Edit: I take that back, I'm not going to check as the problem is non critical as the relevant areas of code are due to be replaced, I think.
BorisTheBrave wrote:
No. Because it might be YOU that is holding a reference to the b2Body, preventing it and the actor from being cleaned up.


But don't the tests I ran indicate that I'm not doing that? I mean, if it was me who was somehow holding the actor objects back (which are the only things that hold a b2Body ref), wouldn't my actor instance count also compound for decorative objects (which have no body) from one level to the next?

My main game class has a "current_level" member. My level is simply a class that holds an array of actors, and some functions that are called on every level change in order to destroy all the bodies and graphics for each actor in that array. The whole level, and therefore the array of actors, is cleaned by calling "game.current_level = new Level()". How could this not release the Actors (and therefore the body references they hold) properly?

Quote:
Edit: I take that back, I'm not going to check as the problem is non critical as the relevant areas of code are due to be replaced, I think.


I look forward to testing out that update, but it would be nice if I could get a definite confirmation on this issue with the current version. (it's been driving me nuts - I just want to know for sure whether its Box2D or not).

Either way, thanks for your time.
I meant that your code might keep a pointer to a body object itself, not the actor. Obviously, without scrutinizing your code, I cannot say, I was just pointing out it's another possible explanation for the observed results. Sounds like your design is simple enough to avoid that, but it's quite easy to accidentally introduce permanent references (such as listeners that are not cleaned up).

Anyway, let's say it is with Box2D. I would recommend adding userData = null as a work around. This is not a full fix as (presumably) the body itself is still not getting cleaned up.
I should add that it might be a deliberate feature - why waste setting member variables to null when we'll get that effect anyway when the objects are recycled.
BorisTheBrave wrote:
I meant that your code might keep a pointer to a body object itself, not the actor. Obviously, without scrutinizing your code, I cannot say, I was just pointing out it's another possible explanation for the observed results. Sounds like your design is simple enough to avoid that, but it's quite easy to accidentally introduce permanent references (such as listeners that are not cleaned up).


As I said, the actor objects are the only things that hold a b2Body ref, so removing the actor should be synonymous with removing the b2Body ref in this case.

Regarding event listeners: I call removeEventListener on every listening function on the actor. Also, if that was the problem, I would see the instance count compound even for decorative objects that don't have a body. This doesn't happen, so that's not the problem here.

Quote:
Anyway, let's say it is with Box2D.


Is there a way to make sure? That would be better, I think.

Quote:
I would recommend adding userData = null as a work around. This is not a full fix as (presumably) the body itself is still not getting cleaned up.


Yea, that's essentially what I did, except that I put that code directly into the DestroyBody function.

Quote:
I should add that it might be a deliberate feature - why waste setting member variables to null when we'll get that effect anyway when the objects are recycled.


Yea, but that "effect" is not happening here, so it's a problem somewhere. Now, for each possibility that was mentioned, in relation to how it could be "something that I'm doing", I responded with why that's not the case, and I additionally described my testing procedure used to determine that.

Is there a flaw in my testing procedure? I mean, is there anything else that you think should be done in order to determine that it's not my fault?

Feel free to make recommendations. I have no trouble running the tests.
milo wrote:
Is there a flaw in my testing procedure? I mean, is there anything else that you think should be done in order to determine that it's not my fault?

Feel free to make recommendations. I have no trouble running the tests.

I think the issue is just that nobody else has noticed this happening, so there's a bit of skepticism about it. Looking over what you've said, I think I misunderstood initially, and it sounds like you've already tracked down your references, so there may be something else going on.

I'm still a bit confused, because at least in Java and C++ (I'm not hugely familiar with the AS3 internals) Box2d itself doesn't even touch body user data, except to return it in response to a user's request. I would think that if the Body is getting GCed, the user data should be GC eligible as well.

Do you know whether or not the bodies are being released with/without the Actors attached? That might help track this down if it is indeed a Box2d bug. b2World.DestroyBody claims to remove all refs to the body, and if something's going wrong there that would be a real bug. Otherwise I'm officially out of ideas; if you could try to reduce this to a simple testbed demonstration or something like that it would probably help, it's always easier to track this sort of thing down with some code.

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