Skip to content

Commit

Permalink
bugfix: some NonCopyable native objects are never freed
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Oct 30, 2024
1 parent f1c5de3 commit 7c54fb2
Show file tree
Hide file tree
Showing 30 changed files with 513 additions and 29 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/BodyFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,26 @@ public boolean shouldCollide(ConstBodyId bodyId) {
return result;
}
// *************************************************************************
// new protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param filterVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long filterVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(filterVa) : null;
setVirtualAddress(filterVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static long createDefaultFilter();

native private static void free(long filterVa);

native private static boolean shouldCollide(long filterVa, long idVa);
}
17 changes: 17 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/BodyLockRead.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,28 @@ public boolean succeededAndIsInBroadPhase() {
return result;
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param lockVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long lockVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(lockVa) : null;
setVirtualAddress(lockVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static long createBodyLockRead(
long interfaceVa, long idVa);

native private static void free(long lockVa);

native private static long getBody(long lockVa);

native private static void releaseLock(long lockVa);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/BodyLockWrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,28 @@ public boolean succeededAndIsInBroadPhase() {
return result;
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param lockVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long lockVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(lockVa) : null;
setVirtualAddress(lockVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static long createBodyLockWrite(
long interfaceVa, long idVa);

native private static void free(long lockVa);

native private static long getBody(long lockVa);

native private static void releaseLock(long lockVa);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/BodyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@ public void init(int maxBodies, int numBodyMutexes,
init(managerVa, maxBodies, numBodyMutexes, mapVa);
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param managerVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long managerVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(managerVa) : null;
setVirtualAddress(managerVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static void activateBodies(long managerVa, long[] idVas);
Expand All @@ -238,6 +253,8 @@ public void init(int maxBodies, int numBodyMutexes,
native private static void draw(long managerVa, long drawSettingsVa,
long physicsSettingsVa, long rendererVa);

native private static void free(long managerVa);

native private static long getBodies(long managerVa);

native private static int getMaxBodies(long managerVa);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,27 @@ public boolean shouldCollide(int bpLayerIndex) {
return result;
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param filterVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long filterVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(filterVa) : null;
setVirtualAddress(filterVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static long createDefaultFilter();

native private static void free(long filterVa);

native private static boolean shouldCollide(
long filterVa, int bpLayerIndex);
}
17 changes: 17 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/BroadPhaseQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ public PhysicsSystem getSystem() {
return (PhysicsSystem) getContainingObject();
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param queryVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long queryVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(queryVa) : null;
setVirtualAddress(queryVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static void castRay(long queryVa, long raycastVa,
Expand All @@ -206,4 +221,6 @@ native private static void collidePoint(long queryVa, float pointX,
native private static void collideSphere(long queryVa, float centerX,
float centerY, float centerZ, float radius, long collectorVa,
long bplFilterVa, long olFilterVa);

native private static void free(long queryVa);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,23 @@ abstract public class CharacterVsCharacterCollision extends NonCopyable {
*/
CharacterVsCharacterCollision() {
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param collisionVa the virtual address of the native object to assign
* (not zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long collisionVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(collisionVa) : null;
setVirtualAddress(collisionVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static void free(long collisionVa);
}
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ public void drawWireUnitSphere(
*/
native public void nextFrame();
// *************************************************************************
// NonCopyable methods
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
Expand All @@ -580,8 +580,7 @@ public void drawWireUnitSphere(
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
@Override
void setVirtualAddress(long rendererVa, boolean owner) {
final void setVirtualAddress(long rendererVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(rendererVa) : null;
setVirtualAddress(rendererVa, freeingAction);
}
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/github/stephengold/joltjni/NonCopyable.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,4 @@ abstract public class NonCopyable extends JoltPhysicsObject {
NonCopyable(long virtualAddress) {
super(virtualAddress);
}
// *************************************************************************
// new protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param virtualAddress the virtual address of the native object to assign
* (not zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
void setVirtualAddress(long virtualAddress, boolean owner) {
Runnable freeingAction = owner ? () -> free(virtualAddress) : null;
setVirtualAddress(virtualAddress, freeingAction);
}
// *************************************************************************
// native private methods

native private static void free(long virtualAddress);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,27 @@ public boolean shouldCollide(int objLayerIndex) {
return result;
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param filterVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long filterVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(filterVa) : null;
setVirtualAddress(filterVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static long createDefaultFilter();

native private static void free(long filterVa);

native private static boolean shouldCollide(
long filterVa, int objLayerIndex);
}
17 changes: 17 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/PhysicsSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,21 @@ public int update(float deltaTime, int collisionSteps,
return result;
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param systemVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long systemVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(systemVa) : null;
setVirtualAddress(systemVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static void addConstraint(long systemVa, long constraintVa);
Expand All @@ -682,6 +697,8 @@ public int update(float deltaTime, int collisionSteps,
native private static void drawBodies(
long systemVa, long settingsVa, long rendererVa);

native private static void free(long systemVa);

native private static void getActiveBodies(
long systemVa, int ordinal, long vectorVa);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,23 @@ abstract public class SerializableObject extends NonCopyable {
SerializableObject(long virtualAddress) {
super(virtualAddress);
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param virtualAddress the virtual address of the native object to assign
* (not zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long virtualAddress, boolean owner) {
Runnable freeingAction = owner ? () -> free(virtualAddress) : null;
setVirtualAddress(virtualAddress, freeingAction);
}
// *************************************************************************
// native private methods

native private static void free(long virtualAddress);
}
17 changes: 17 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/ShapeFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,24 @@ public ShapeFilter() {
setVirtualAddress(filterVa, true);
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param filterVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long filterVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(filterVa) : null;
setVirtualAddress(filterVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static long createDefaultFilter();

native private static void free(long filterVa);
}
17 changes: 17 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/StateRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,25 @@ public void write(Vec3Arg v) {
writeVec3(recorderVa, x, y, z);
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param recorderVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long recorderVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(recorderVa) : null;
setVirtualAddress(recorderVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static void free(long recorderVa);

native private static boolean isValidating(long recorderVa);

native private static void readBodyIdVector(long recorderVa, long vectorVa);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/StreamOut.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,23 @@ abstract public class StreamOut extends NonCopyable {
*/
StreamOut() {
}
// *************************************************************************
// protected methods

/**
* Assign a native object, assuming there's none already assigned.
*
* @param streamVa the virtual address of the native object to assign (not
* zero)
* @param owner true → make the JVM object the owner, false → it
* isn't the owner
*/
final void setVirtualAddress(long streamVa, boolean owner) {
Runnable freeingAction = owner ? () -> free(streamVa) : null;
setVirtualAddress(streamVa, freeingAction);
}
// *************************************************************************
// native private methods

native private static void free(long streamVa);
}
Loading

0 comments on commit 7c54fb2

Please sign in to comment.