Possible causes for this error?

Official forum for the Chipmunk2D Physics Library.
snichols
Posts: 53
Joined: Mon Nov 12, 2012 9:20 pm
Contact:

Possible causes for this error?

Post by snichols »

I'm getting this assertion failure: "Internal Error: Dangling root pointer detected in contact graph."

Can you help me better understand what would cause this to happen? :)

steve
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Possible causes for this error?

Post by slembcke »

Only sleeping bodies should have anything assigned to their next/root "node" pointers outside of the cpSpaceProcessComponents() function. Either they didn't get reset for some reason (the very last lines of that function), or you have more memory corruption issues. Honestly, I haven't seen that assertion happen since I was developing the sleeping algorithm.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
snichols
Posts: 53
Joined: Mon Nov 12, 2012 9:20 pm
Contact:

Re: Possible causes for this error?

Post by snichols »

So, I'm fairly certain I've resolved my corruption issues. However, I'm seeing this assert on occasion as well as the invalid pointer error I referenced earlier. For a refresher, it's happening when freeing arb->contacts at line 66 of cpSpaceComponent.cpp. The memory library I'm using doesn't recognize the pointer being passed in as valid.

I'll need to do some extra work to log allocations and whatnot. I can imagine that I'm still not doing something right on object cleanup in some edge case. But, my only indication of any such problem is this error. I receive the aforementioned assert after I get the contacts is not a valid pointer error and then continue running. So, it sure looks like a symptom of the invalid pointer issue.

I'll add logging / extra pointer tracking to see where this is going wrong. More info soon.

steve
snichols
Posts: 53
Joined: Mon Nov 12, 2012 9:20 pm
Contact:

Re: Possible causes for this error?

Post by snichols »

Okay, I have more information on this. From my point of view, it doesn't seem to be memory corruption. So, that's good news.

The short version: arb->contacts, which is referenced by line 66 of cpSpaceComponent.cpp, is pointing to a cpContact inside of a cpContactBuffer instance. So, when cpfree(contacts) is called, the pointer isn't correct.

I found this by adding a sentinel integer to cpContact and detecting when the invalid free takes place. Then, backtracking from the bogus contacts pointer until I found the beginning of the memory block.

I'm unsure what I could be doing to cause this to happen. Any guidance on what might cause this to happen in theory would be handy!

Thanks...

steve
snichols
Posts: 53
Joined: Mon Nov 12, 2012 9:20 pm
Contact:

Re: Possible causes for this error?

Post by snichols »

A bit more info...

It seems the errors are being caused by calling cpBodyApplyImpulse within a cpSpaceShapeQuery callback. Is there any reason why this might be an issue? Is cpBodyApplyImpulse safe to call within callbacks?

steve
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Possible causes for this error?

Post by slembcke »

Oh. Hrm. That would mean that cpSpaceActivateBody() is being called on a body that isn't actually sleeping. It seems very likely that this and the first error are related though. There isn't a specific flag for if a body is sleeping. Only sleeping bodies should have the pointers in the cpBody.node struct filled in. cpBodyIsSleeping() actually just checks that. So if the node pointer is wrong, it will think the body is sleeping and attempt to wake it up, even though it was never put to sleep.

I have no idea why that would be happening though. The loop starting at cpSpaceComponent.c:274 loops over all the bodies and uses the node pointers to find connected components in the contact graph. At the end, of the loop, either all of the bodies will be put to sleep or they will have their node pointers reset to NULL. The only time the node pointers are set is from that loop or when calling cpBodySleepWithGroup() or cpBodySleep().

Are the node pointers set to a valid body?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Possible causes for this error?

Post by slembcke »

Oops. A bit of cross posting.

That is fine to call from a callback. It's just the case where most functions that modify bodies wake them up as it may be invalidating the conditions that allowed them to sleep.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
snichols
Posts: 53
Joined: Mon Nov 12, 2012 9:20 pm
Contact:

Re: Possible causes for this error?

Post by snichols »

It looks like the bodies are correct, yes.

To be clear, this cpSpaceShapeQuery is being invoked from within an arbitrary collision callback. The results of that query are then used to apply impulse. I'm doing this to generate explosive force at arbitrary world locations.

When I queue up these explosions and process them at the beginning of the frame then the crashes and oddities go away. Curious. :)

steve
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Possible causes for this error?

Post by slembcke »

Hmm. No idea why that would make any difference. There are no callbacks triggered inside of cpSpaceProcessComponents(), so it shouldn't matter.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
snichols
Posts: 53
Joined: Mon Nov 12, 2012 9:20 pm
Contact:

Re: Possible causes for this error?

Post by snichols »

Some more details... Consider line 192 of cpSpaceComponent.cpp:

Code: Select all

CP_BODY_FOREACH_ARBITER(body, arb) FloodFillComponent(root, (body == arb->body_a ? arb->body_b : arb->body_a));
When problems begin, It seems that arb->body_a has been freed already. I'm fairly certain that I remove bodies from the space / etc. before freeing them. Is there something else I should be doing to ensure that arbiter body pointers are cleaned up properly?

steve
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests