C
C#13mo ago
nut

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
nut
nutOP13mo ago
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)
this is the problematic code:
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);
all it does is convert a list of my own triangles to Jolt's triangles, use it to create a mesh, then create body settings from that mesh, and then create and add the body to the world the class Jolt follows a singleton pattern:
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;
}
the constructor looks like this, mostly just the HelloWorld example:
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();
}
im just wondering if physics system is getting disposed of somehow it shouldn't though? hold up, i removed the using keywords and it works now that was from the helloworld example
Want results from more Discord servers?
Add your server