JoltPhysicsSharp access violation
Hi, I'm trying to use JoltPhysicsSharp in my project, and I started with the HelloWorld.cs example provided, and it works fine, but when I try to call CreateAndAddBody again, it gives me an access violation
1 Reply
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
at JoltPhysicsSharp.JoltApi.JPH_BodyInterface_CreateAndAddBody(IntPtr, IntPtr, JoltPhysicsSharp.Activation)
--------------------------------
at JoltPhysicsSharp.BodyInterface.CreateAndAddBody(JoltPhysicsSharp.BodyCreationSettings, JoltPhysicsSharp.Activation)
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
at JoltPhysicsSharp.JoltApi.JPH_BodyInterface_CreateAndAddBody(IntPtr, IntPtr, JoltPhysicsSharp.Activation)
--------------------------------
at JoltPhysicsSharp.BodyInterface.CreateAndAddBody(JoltPhysicsSharp.BodyCreationSettings, JoltPhysicsSharp.Activation)
MeshShapeSettings settings = new MeshShapeSettings(marchingCubes.getTriangles().Select(e => e.ToPhysicsTriangle()).ToArray());
BodyCreationSettings bodySettings = new BodyCreationSettings(settings, new Vector3(Position.X * World.CHUNK_SIZE, 0, Position.Y * World.CHUNK_SIZE), Quaternion.Identity, MotionType.Static, Jolt.Layers.NonMoving);
Jolt.BodyInterface.CreateAndAddBody(bodySettings, Activation.DontActivate);
MeshShapeSettings settings = new MeshShapeSettings(marchingCubes.getTriangles().Select(e => e.ToPhysicsTriangle()).ToArray());
BodyCreationSettings bodySettings = new BodyCreationSettings(settings, new Vector3(Position.X * World.CHUNK_SIZE, 0, Position.Y * World.CHUNK_SIZE), Quaternion.Identity, MotionType.Static, Jolt.Layers.NonMoving);
Jolt.BodyInterface.CreateAndAddBody(bodySettings, Activation.DontActivate);
public static Jolt Instance { get; private set; }
private BodyInterface bodyInterface;
public static BodyInterface BodyInterface {
get {
return Instance.bodyInterface;
}
}
private PhysicsSystem physicsSystem;
public static PhysicsSystem PhysicsSystem { get => Instance.physicsSystem; }
public static Jolt Initialize()
{
if(Instance == null)
{
Instance = new Jolt();
}
return Instance;
}
public static Jolt Instance { get; private set; }
private BodyInterface bodyInterface;
public static BodyInterface BodyInterface {
get {
return Instance.bodyInterface;
}
}
private PhysicsSystem physicsSystem;
public static PhysicsSystem PhysicsSystem { get => Instance.physicsSystem; }
public static Jolt Initialize()
{
if(Instance == null)
{
Instance = new Jolt();
}
return Instance;
}
private Jolt()
{
if (!Foundation.Init(true))
{
return;
}
// Malloc
using TempAllocator tempAllocator = new(10 * 1024 * 1024);
using JobSystemThreadPool jobSystem = new(Foundation.MaxPhysicsJobs, Foundation.MaxPhysicsBarriers);
using BPLayerInterfaceImpl broadPhaseLayer = new();
using ObjectVsBroadPhaseLayerFilterImpl objectVsBroadphaseLayerFilter = new();
using ObjectLayerPairFilterImpl objectVsObjectLayerFilter = new();
physicsSystem = new();
physicsSystem.Init(MaxBodies, NumBodyMutexes, MaxBodyPairs, MaxContactConstraints,
broadPhaseLayer,
objectVsBroadphaseLayerFilter,
objectVsObjectLayerFilter);
// ContactListener
physicsSystem.OnContactValidate += OnContactValidate;
physicsSystem.OnContactAdded += OnContactAdded;
physicsSystem.OnContactPersisted += OnContactPersisted;
physicsSystem.OnContactRemoved += OnContactRemoved;
// BodyActivationListener
physicsSystem.OnBodyActivated += OnBodyActivated;
physicsSystem.OnBodyDeactivated += OnBodyDeactivated;
bodyInterface = physicsSystem.BodyInterface;
//Foundation.Shutdown();
}
private Jolt()
{
if (!Foundation.Init(true))
{
return;
}
// Malloc
using TempAllocator tempAllocator = new(10 * 1024 * 1024);
using JobSystemThreadPool jobSystem = new(Foundation.MaxPhysicsJobs, Foundation.MaxPhysicsBarriers);
using BPLayerInterfaceImpl broadPhaseLayer = new();
using ObjectVsBroadPhaseLayerFilterImpl objectVsBroadphaseLayerFilter = new();
using ObjectLayerPairFilterImpl objectVsObjectLayerFilter = new();
physicsSystem = new();
physicsSystem.Init(MaxBodies, NumBodyMutexes, MaxBodyPairs, MaxContactConstraints,
broadPhaseLayer,
objectVsBroadphaseLayerFilter,
objectVsObjectLayerFilter);
// ContactListener
physicsSystem.OnContactValidate += OnContactValidate;
physicsSystem.OnContactAdded += OnContactAdded;
physicsSystem.OnContactPersisted += OnContactPersisted;
physicsSystem.OnContactRemoved += OnContactRemoved;
// BodyActivationListener
physicsSystem.OnBodyActivated += OnBodyActivated;
physicsSystem.OnBodyDeactivated += OnBodyDeactivated;
bodyInterface = physicsSystem.BodyInterface;
//Foundation.Shutdown();
}