crashes when a Ragdoll points to a destroyed PhysicsSystem #1431
-
As I mentioned before, I'm creating Java bindings for JoltPhysics. Earlier this month I began experiencing various crashes:
After much experimentation, I've narrowed down the issue. I believe it relates to the order in which destructors for As you're probably aware, Java programmers aren't accustomed to thinking about object destruction. When using my Java bindings, C++ objects like physics systems and ragdoll references are allocated on the heap, and their destructors typically get invoked by an asynchronous automatic cleanup thread. In this scenario, the Java application cannot control the order in which those destructors are invoked. It's entirely possible for This scenario is unlikely in C++ applications, where typical coding practices ensure destruction of all ragdolls prior to destruction of the physics system(s) they point to. I ran an experiment where I disabled my cleanup thread so I could specify the destruction sequence. When the sequence was: for (RagdollRef r : mRagdolls)
r.close();
mPhysicsSystem.close(); the application always completed successfully, but when the sequence was: mPhysicsSystem.close();
for (RagdollRef r : mRagdolls)
r.close(); the application crashed every time at Jolt/Core/Array.h:455: It ought to be possible to modify the |
Beta Was this translation helpful? Give feedback.
Replies: 12 comments 5 replies
-
Unfortunately this is not something that can be fixed. The PhysicsSystem does not know about the existance of the Ragdoll so it cannot destroy it first. Ragdoll is a higher level concept that the physics system should not know about. In general Jolt tries to avoid links between objects as much as possible. This is one of the main reasons why you can do many things in a thread safe way. |
Beta Was this translation helpful? Give feedback.
-
I understand. |
Beta Was this translation helpful? Give feedback.
-
That's not a proper solution either. You can do:
This is quite common as a character may only need an active ragdoll during certain situations (e.g. hit responses), but you want to keep the ragdoll around because it's expensive to create. I don't think this is going to be the only case where destruction order matters and I would suggest looking into a more generic system to handle this. I took a quick look at jolt-jni:
My Java skills are pretty low so excuse me if my suggestions don't make sense. |
Beta Was this translation helpful? Give feedback.
-
That makes sense.
I already track dependencies for C++ objects contained in other C++ objects, such as vectors. However, tracking doesn't help with this issue because it only prevents containers from being cleaned while their contents are strongly reachable. In this issue, neither the ragdoll nor the physics space is strongly reachable.
I don't know of any way to do this. I'll ask around. |
Beta Was this translation helpful? Give feedback.
-
Question: are there other situations in Jolt Physics where the destructor of a high-level object refers to a low-level object with its own destructor? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the brainstorm. |
Beta Was this translation helpful? Give feedback.
-
The
Yes, I need to control the destruction order for
The (to be continued) |
Beta Was this translation helpful? Give feedback.
-
The
Yes, I need to control the destruction order for |
Beta Was this translation helpful? Give feedback.
-
Actually it being a ref count target is wrong and I've created #1441 to fix that. Both the wheels and controller are created in the constructor of the VehicleConstraint and they're destroyed in the destructor. They can't be shared with other constraints, so I think you can simply not destruct objects of these types as you're never going to be the owner (and if you don't expose their constructors in Java then users can't create them accidentally either). |
Beta Was this translation helpful? Give feedback.
-
That makes sense. Thank you for the PR. I'll resume work on this after it's integrated into the JoltPhysics "master" branch. |
Beta Was this translation helpful? Give feedback.
-
I've handled
I don't see where that occurs in the code.
I greatly appreciate your help. I'll keep my eyes open for similar situations. For the record, the technique I'm using to control destruction order (for |
Beta Was this translation helpful? Give feedback.
-
No worries. I merely wanted to make sure I wasn't missing something. Thanks for double checking.
Along with the Java object, I pass the virtual address of the Last night I released jolt-jni v0.9.4 . Thanks all for your help, Jorrit! |
Beta Was this translation helpful? Give feedback.
Unfortunately this is not something that can be fixed. The PhysicsSystem does not know about the existance of the Ragdoll so it cannot destroy it first. Ragdoll is a higher level concept that the physics system should not know about. In general Jolt tries to avoid links between objects as much as possible. This is one of the main reasons why you can do many things in a thread safe way.