removing lots of statics crash

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
lidgren
Posts: 21
Joined: Mon May 27, 2019 6:28 pm

removing lots of statics crash

Post by lidgren » Thu Apr 09, 2020 10:51 pm

Hi!
I have a mysterious crash that I'm unable to track down the reason for. This is the relevant part of the callstack:

System.Private.CoreLib.dll!System.Diagnostics.DebugProvider.Fail(string message, string detailMessage) Line 32
BepuUtilities.dll!BepuUtilities.Memory.Buffer<BepuPhysics.Collidables.Collidable>.this[int].get(int index) Line 56
BepuPhysics.dll!BepuPhysics.Statics.RemoveAt(int index) Line 184
BepuPhysics.dll!BepuPhysics.Statics.Remove(int handle) Line 224

(The assert triggers because HandleToIndex[handle] is -1)

I've verified that the handle has been added properly and is not removed twice. I'm removing a whole bunch of statics in a loop and it fails after deleting a handful of them; it's deterministic and reproable.

Understanding the statics tree and handle/index thing is tricky; but looking at Statics.RemoveAt it seems it's deleting the next-to-last leaf, causing the last leaf to move up into its place.. but leaving HandleToIndex for the handle pointing at the old location; now outside the tree.

If I misunderstand the code and it's all fine; what could cause this crash and how can I figure out what's happening?

Thanks again for your great support of this library!

--michael

Norbo
Site Admin
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: removing lots of statics crash

Post by Norbo » Fri Apr 10, 2020 12:52 am

HandleToIndex[handle] == -1 is indeed an implication that the was never added or was already removed, as you mentioned. Nothing else should cause that error under normal conditions. It can also happen due to asynchronous corruption- either multiple threads working on adds/removes simultaneously or adding/removing to the simulation while it's running.

I'm attempting to reproduce in the demos and can't so far. FountainStressTestDemo (which covers constant randomized add/removes) passes for as long as I care to run it too.

I'll probably need a repro in the demos to look at to figure it out.

lidgren
Posts: 21
Joined: Mon May 27, 2019 6:28 pm

Re: removing lots of statics crash

Post by lidgren » Fri Apr 10, 2020 12:23 pm

Some more debugging reveals the problem to be that Statics.broadPhase.leaves[42] and Statics.broadPhase.leaves[53] both point to handle 49. When the first broadphase index is removed; HandleToIndex[49] gets set to -1; causing the removal of broadphase index 53 to blow up.

The reason it exists twice is that it gets added once simulation.Statics.Add and then again when I apply a description via simulation.Statics.ApplyDescription.

Is multiple entries in the broadphase tree valid or invalid? If invalid; then perhaps a check in ApplyDescriptionByIndex is required? Or I might have misunderstood... there's a lot of lists, indices and handles that are hard to reason about with my limited knowledge :-)

(or are there restrictions around ApplyDescription I need to adher to?)

Norbo
Site Admin
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: removing lots of statics crash

Post by Norbo » Fri Apr 10, 2020 7:19 pm

There we go, reproduced- static ApplyDescription is busted. Working on it now.

Norbo
Site Admin
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: removing lots of statics crash

Post by Norbo » Fri Apr 10, 2020 9:06 pm

Should be fixed in https://github.com/bepu/bepuphysics2/co ... 4f4f1aad63. Still needs a bit more testing (and there's an inconsistency in behavior with bodies that needs to be addressed), but the core crash should be gone.

lidgren
Posts: 21
Joined: Mon May 27, 2019 6:28 pm

Re: removing lots of statics crash

Post by lidgren » Fri Apr 10, 2020 9:18 pm

This patch solves my issues completely. Awesome support again; thanks!

Post Reply