Kraft crash removing objects?

I have been getting occasional crashes, and I think I see what triggers it. If a road segment graphic is deleted from the scene and a vehicle is touching that graphic, it can crash. In this case I am adding a segment (orange) and the building of the intersection on the left point is triggering the segment the tan van is on to rebuild so that it will connect properly to the intersection. When that rebuilds, it deletes the old one … and because that tan van is touching it, it crashes. Is there a safer way to insure that removing physics objects from the scene won’t crash kraft? It doesn’t happen all the time.

From the stack trace in the above screenshot, indeed it seems something was left in Kraft, but it should have been released. And at physics processing (which happens from TCastleAbstractRootTransform.Update) Kraft crashes trying to access non-existing object.

In general, in CGE, you should not have to do anything special. You just free the TCastleTransform, or remove it from world, or remove the TCastleRigidBody or collider – all those things should release Kraft resources associated with it.

Can you provide a minimal testcase to reproduce this? I know it may be non-trivial, but we will need it to fix this. Note that we also have PR that updates Kraft to later version ( New physics based first/third person 3d and platformer like modular navigation + input axis and others by and3md · Pull Request #533 · castle-engine/castle-engine · GitHub ), so it is possible the issue is fixed at Kraft level in latest version. But it will take me still some time before I get to applying this PR, I planned to do this after 7.0-alpha3 release.

I am unable to reliably reproduce the crash which makes it difficult.

I believe the ultimate source for this problem is my own code allowing ‘phantom’ road nodes and segments to get created despite when it was supposed to be cancelled due to validation failure or pressing esc. Then the phantom nodes/segments lead to problems because they were deleted but still somehow in my road network. And this manifests with the kraft error when a change to the network triggers a phantom to rebuild where a vehicle is. I am still figuring it out but I don’t think it is an engine error.