Skip to content

Commit

Permalink
PhysicsSystem: protect the contact listener against GC
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Sep 25, 2024
1 parent 337cda7 commit 57fe9bb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 29 deletions.
24 changes: 8 additions & 16 deletions src/main/java/com/github/stephengold/joltjni/PhysicsSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class PhysicsSystem extends NonCopyable {
* cached reference to the system's no-lock {@code BodyInterface}
*/
final private BodyInterface bodyInterfaceNoLock;
/**
* protect the contact listener (if any) from garbage collection
*/
private ContactListener contactListener;
/**
* protect the BroadPhaseLayerInterface from garbage collection
*/
Expand Down Expand Up @@ -292,23 +296,12 @@ public Constraints getConstraints() {
}

/**
* Access the system's {@code ContactListener}.
* Access the (application-provided) contact listener.
*
* @return a new JVM object with the pre-existing native object assigned, or
* {@code null} if none
* @return the pre-existing instance, or {@code null} if none
*/
public ContactListener getContactListener() {
long systemVa = va();
long listenerVa = getContactListener(systemVa);

ContactListener result;
if (listenerVa == 0L) {
result = null;
} else {
result = new ContactListener(listenerVa, false);
}

return result;
return contactListener;
}

/**
Expand Down Expand Up @@ -558,6 +551,7 @@ public void setCombineRestitution(CombineFunction function) {
* @param listener the desired listener
*/
public void setContactListener(ContactListener listener) {
this.contactListener = listener;
long systemVa = va();
long listenerVa = listener.va();
setContactListener(systemVa, listenerVa);
Expand Down Expand Up @@ -646,8 +640,6 @@ native private static void getActiveBodies(

native private static long getConstraints(long systemVa);

native private static long getContactListener(long systemVa);

native private static float getGravityX(long systemVa);

native private static float getGravityY(long systemVa);
Expand Down
13 changes: 0 additions & 13 deletions src/main/native/glue/p/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,6 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_PhysicsSystem_getCon
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_PhysicsSystem
* Method: getContactListener
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_PhysicsSystem_getContactListener
(JNIEnv *, jclass, jlong systemVa) {
const PhysicsSystem * const pSystem
= reinterpret_cast<PhysicsSystem *> (systemVa);
ContactListener * const pResult = pSystem->GetContactListener();
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_PhysicsSystem
* Method: getGravityX
Expand Down

0 comments on commit 57fe9bb

Please sign in to comment.