AccessViolationException (C#)

sooms
Posts: 6
Joined: Wed Jun 06, 2012 2:41 am

AccessViolationException (C#)

Post by sooms »

Hey all.

I'm currently converting the PhysX implementation to Bullet for my project but I have hit an issue. When I try to run world.StepSimulation(timeSinceLastFrame / 1000) I get an AccessViolationException. I have tried changing the objects added to the world to see if any of them were the issue but the error is always the same. I am using BulletSharp 2.8 SP1 (which I think is pretty much up to date with the current bullet release) and I am using c# which is different from the core bullet code but since there is no real forum for bulletsharp I thought I would try here. The world setup code is:

Code: Select all

CollisionConfiguration configuration = new DefaultCollisionConfiguration();
            Dispatcher dispatcher = new CollisionDispatcher(configuration);
            DbvtBroadphase broadphase = new DbvtBroadphase();
            world = new DiscreteDynamicsWorld(dispatcher, broadphase, null, configuration);
            world.Gravity = new Vector3(0, gravity, 0);
And the code I use to add my character controller is:

Code: Select all

ConvexShape capsule = new CapsuleShape(radius, modelHeight - radius * 2);
            PairCachingGhostObject ghostObject = new PairCachingGhostObject();
            Matrix4 worldTransform;
            Matrix4.MakeTransform(characterNode.Position, Vector3.UnitScale, characterNode.Orientation, out worldTransform);
            ghostObject.WorldTransform = worldTransform;
	    ghostObject.CollisionShape = capsule;
	    ghostObject.CollisionFlags = CollisionFlags.CharacterObject;
            playerController = new KinematicCharacterController(ghostObject, capsule, 500f / scaleFactor);
            world.AddCollisionObject(ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter);
            world.AddAction(playerController);
And then I call the step function to get things going:

Code: Select all

world.StepSimulation(timeSinceLastFrame / 1000);
Is there anything obviously wrong with this? Would anyone have any suggestions on what I could try?

Thanks,
Andrew.
anthrax11
Posts: 72
Joined: Wed Feb 24, 2010 9:49 pm

Re: AccessViolationException (C#)

Post by anthrax11 »

Hi.

The code seems fine to me, at least I don't get any exception with it.

A common cause for the AccessViolationException is when some object is disposed prematurely (e.g. by going out of scope) while it is still used by the collision world. The dispose method deletes the object that is wrapped and the simulation will try to access a destroyed object. It doesn't seem to be the case in this code since DiscreteDynamicsWorld keeps a reference to the broadphase, dispatcher and conf objects and you keep a reference to the playerController yourself. Not sure about the ghostObject though.

You can try looking at the UnmanagedPointer property of collision objects when you hit the exception to see if they point to valid locations. If they are 0, then it usually means the object was disposed. If they are invalid, then you should see the AccessViolationException again, which means the object was destroyed in some other way.

Does it also fail if no collision objects or controllers are added to the world?
sooms
Posts: 6
Joined: Wed Jun 06, 2012 2:41 am

Re: AccessViolationException (C#)

Post by sooms »

Ok I made the ghostpairobject a class wide variable so it wont go out of scope. I then ran it again and still got the error so I checked the characters collision objects unmanaged pointer, it was:

UnmanagedPointer = 0x06a76fc0 - *((BulletSharp.CollisionObject)((new System.Collections.ArrayL...nWorld.CollisionObjectArray)).Items)).Items[0])).UnmanagedPointer = {btCollisionObject}

I usually get a FatalExecutionEngineError if I try to run it with no objects/characters in the world.

One other thing I noticed, when viewing the properties of the world class I notice that Broadphase is null. I changed the broadphase property to be stored by the class but it is still null. I'm going to guess it shouldn't be null right?

Thanks for all your help so far, I'll keep digging and will keep checking back here for any advice you or others might have.

UPDATE:

By changing:
DiscreteDynamicsWorld world;
to
DynamicsWorld world;

The broadphase is no longer null. However this hasn't fixed the issue that I am seeing.

UPDATE 2:
I removed all of my objects from the world then added the ground shape that is used in the basic demo. This seemed to result in the program no longer crashing, so at least I have managed to get it running :) Now to get it working with my creations...
sooms
Posts: 6
Joined: Wed Jun 06, 2012 2:41 am

Re: AccessViolationException (C#)

Post by sooms »

A final update. I decided to try use Bullet-XNA instead and it works fine. I'm not too sure what the problem with BulletSharp was and I guess I will never know :p
anthrax11
Posts: 72
Joined: Wed Feb 24, 2010 9:49 pm

Re: AccessViolationException (C#)

Post by anthrax11 »

It may be related to this issue, which I've just fixed :) http://code.google.com/p/bulletsharp/is ... tail?id=33

edit: Any chance you can send me the project that fails?
sooms
Posts: 6
Joined: Wed Jun 06, 2012 2:41 am

Re: AccessViolationException (C#)

Post by sooms »

That sounds very much like the issue. I wont be able to send you a copy of the project at the moment for testing as it is commercially sensitive code and it's for a MMO so the amount of code is massive (and I haven't got a small single player test version set up yet (which I really should do).

If you want to send me a copy that has the fix for Axiom then I can give it a try for you and verify it. I will add though that I realised I can't use Bulletsharp anyway due to it not being fully managed code (which I need to run in a silverlight environment).
anthrax11
Posts: 72
Joined: Wed Feb 24, 2010 9:49 pm

Re: AccessViolationException (C#)

Post by anthrax11 »

Ok, no problem. Here is 2.80 SP1 with the fix. http://bulletsharp.googlecode.com/files ... 80-sp1.zip
amrishkum
Posts: 17
Joined: Wed May 23, 2012 7:08 pm

Re: AccessViolationException (C#)

Post by amrishkum »

Hey I had the same problem. It is because of memory leaks. Make sure delete the objects in C++ after you create it in the opposite order of creation.
sooms
Posts: 6
Joined: Wed Jun 06, 2012 2:41 am

Re: AccessViolationException (C#)

Post by sooms »

Good news everyone!

That seems to have fixed the issue. Now for me to fine tune the world to make it work as I want.

Thanks :)