Skip to content

Commit

Permalink
bugfix: possible premature garbage collection of VehicleConstraint
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jan 6, 2025
1 parent e19afcd commit cae18db
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -128,7 +128,7 @@ public Wheel getWheel(int wheelIndex) {
long constraintVa = va();
long wheelVa = getWheel(constraintVa, wheelIndex);
int ordinal = Constraint.getControllerType(constraintVa);
Wheel result = Wheel.newWheel(wheelVa, ordinal);
Wheel result = Wheel.newWheel(wheelVa, ordinal, this);

return result;
}
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/github/stephengold/joltjni/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -41,13 +41,15 @@ public class Wheel extends NonCopyable {
// constructors

/**
* Instantiate with the specified native object assigned but not owned.
* Instantiate with the specified container and native object.
*
* @param container the containing object, or {@code null} if none
* @param wheelVa the virtual address of the native object to assign (not
* zero)
*/
Wheel(long wheelVa) {
setVirtualAddress(wheelVa);
Wheel(VehicleConstraint container, long wheelVa) {
super(container, wheelVa);

long settingsVa = getSettings(wheelVa);
if (this instanceof WheelTv) {
this.settings = new WheelSettingsTv(settingsVa).toRef();
Expand Down Expand Up @@ -75,9 +77,11 @@ public ConstWheelSettings getSettings() {
*
* @param wheelVa the virtual address of the native object, or zero
* @param ordinal the type of {@code VehicleController}
* @param container the containing object, or {@code null} if none
* @return a new JVM object, or {@code null} if {@code wheelVa} was zero
*/
public static Wheel newWheel(long wheelVa, int ordinal) {
public static Wheel newWheel(
long wheelVa, int ordinal, VehicleConstraint container) {
if (wheelVa == 0L) {
return null;
}
Expand All @@ -86,10 +90,10 @@ public static Wheel newWheel(long wheelVa, int ordinal) {
switch (ordinal) {
case VehicleController.motorcycleType:
case VehicleController.wheeledVehicleType:
result = new WheelWv(wheelVa);
result = new WheelWv(container, wheelVa);
break;
case VehicleController.trackedVehicleType:
result = new WheelTv(wheelVa);
result = new WheelTv(container, wheelVa);
break;
default:
throw new IllegalArgumentException("ordinal = " + ordinal);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/github/stephengold/joltjni/WheelTv.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,12 +31,13 @@ public class WheelTv extends Wheel {
// constructors

/**
* Instantiate with the specified native object assigned but not owned.
* Instantiate with the specified container and native object.
*
* @param container the containing object, or {@code null} if none
* @param wheelVa the virtual address of the native object to assign (not
* zero)
*/
WheelTv(long wheelVa) {
super(wheelVa);
WheelTv(VehicleConstraint container, long wheelVa) {
super(container, wheelVa);
}
}
9 changes: 5 additions & 4 deletions src/main/java/com/github/stephengold/joltjni/WheelWv.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,12 +31,13 @@ public class WheelWv extends Wheel {
// constructors

/**
* Instantiate with the specified native object assigned but not owned.
* Instantiate with the specified container and native object.
*
* @param container the containing object, or {@code null} if none
* @param wheelVa the virtual address of the native object to assign (not
* zero)
*/
WheelWv(long wheelVa) {
super(wheelVa);
WheelWv(VehicleConstraint container, long wheelVa) {
super(container, wheelVa);
}
}

0 comments on commit cae18db

Please sign in to comment.