Search code examples
c#crashbulletphysics

BulletSharp crashing on application exit


Using the following startup:

        BulletSharp.DefaultCollisionConstructionInfo collisionConstructionInfo =
            new BulletSharp.DefaultCollisionConstructionInfo();
        BulletSharp.DefaultCollisionConfiguration collisionConfiguration =
            new BulletSharp.DefaultCollisionConfiguration(
                collisionConstructionInfo );
        BulletSharp.Dispatcher collisionDispatcher =
            new BulletSharp.CollisionDispatcher(
                collisionConfiguration );
        BulletSharp.BroadphaseInterface broadPhaseCollisionInterface =
            new BulletSharp.SimpleBroadphase( );
        BulletSharp.CollisionWorld bulletCollisionWorld =
            new BulletSharp.CollisionWorld(
                collisionDispatcher,
                broadPhaseCollisionInterface,
                collisionConfiguration );

        BulletSharp.ConstraintSolver constraintSolver =
            new BulletSharp.SequentialImpulseConstraintSolver();
        BulletSharp.DynamicsWorld bulletDynamicsWorld =
            new BulletSharp.DiscreteDynamicsWorld(
                collisionDispatcher,
                broadPhaseCollisionInterface,
                constraintSolver,
                collisionConfiguration );

Running this sixty times a second:

bulletDynamicsWorld.StepSimulation( (float)deltaTime, 9, 1.0F / 40.0F );

Then calling these on exit some arbitrary point later:

        Utility.SafeDispose( bulletDynamicsWorld );
        Utility.SafeDispose( constraintSolver );
        Utility.SafeDispose( broadPhaseCollisionInterface );
        Utility.SafeDispose( collisionDispatcher );
        Utility.SafeDispose( collisionConfiguration );
        Utility.SafeDispose( bulletCollisionWorld ); <<< The error happens here >>>

I get the following error executing the highlighted line:

"The runtime has encountered a fatal error. The address of the error was at 0x6b1c9704, on thread 0x1378. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack."

Notes:

1) That is all the bullet code there is. No collision objects or dynamic objects have been added.

2) Utility.SafeDispose() takes an IDiposable, checks for null value, calls .Dispose() if its valid.

3) The language is C#, to make it clear.

4) Position of Utility.SafeDispose( CollisionWorld ) in the list of .SafeDispose statements does not seem to have an effect.

Why is it crashing, and how do I fix it?

Thanks.


Solution

  • When disposing bulletCollisionWorld, it will access broadPhaseCollisionInterface to clear any collision pairs that were created by the world. Since you explicitly dispose the collision interface before disposing the world, then the world will access an invalid pointer.

    So the solution is to dispose both of the worlds first and then the collision interface.