Skip to content

Commit

Permalink
fix: Properly create a strong reference to a pointer when doing `asSt…
Browse files Browse the repository at this point in the history
…ackElement`
  • Loading branch information
Berstanio committed Jan 15, 2025
1 parent b53e540 commit 8a0bcf6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class GCHandler {
private static final boolean NO_GC_FREE = System.getProperty("com.badlogic.jnigen.gc_disabled", "false").equals("true");
private static final boolean ENABLE_GC_LOG = System.getProperty("com.badlogic.jnigen.gc_log", "false").equals("true");
protected static final ReferenceQueue<Pointing> REFERENCE_QUEUE = new ReferenceQueue<>();
protected static final ReferenceQueue<Object> REFERENCE_QUEUE = new ReferenceQueue<>();
private static final Set<PointingPhantomReference> referenceHolder = Collections.synchronizedSet(new HashSet<PointingPhantomReference>());
private static final Map<Long, AtomicInteger> countMap = new HashMap<>();

Expand Down Expand Up @@ -59,22 +59,22 @@ public void uncaughtException(Thread t, Throwable e) {
RELEASER.start();
}

public static void enqueuePointer(Pointing pointing) {
public static void enqueuePointer(Object pointing, long pointer) {
if (NO_GC_FREE)
return;
if (ENABLE_GC_LOG)
System.out.println("Enqueuing Pointer: " + pointing.getPointer());
System.out.println("Enqueuing Pointer: " + pointer);

synchronized (countMap) {
AtomicInteger counter = countMap.get(pointing.getPointer());
AtomicInteger counter = countMap.get(pointer);
if (counter == null) {
counter = new AtomicInteger(0);
countMap.put(pointing.getPointer(), counter);
countMap.put(pointer, counter);
}
counter.incrementAndGet();
}

PointingPhantomReference structPhantomReference = new PointingPhantomReference(pointing);
PointingPhantomReference structPhantomReference = new PointingPhantomReference(pointing, pointer);
referenceHolder.add(structPhantomReference);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import java.lang.ref.PhantomReference;

public class PointingPhantomReference extends PhantomReference<Pointing> {
public class PointingPhantomReference extends PhantomReference<Object> {

private final long pointer;
public PointingPhantomReference(Pointing referent) {
public PointingPhantomReference(Object referent, long pointer) {
super(referent, GCHandler.REFERENCE_QUEUE);
this.pointer = referent.getPointer();
this.pointer = pointer;
}

public long getPointer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Pointing(long pointer, boolean freeOnGC) {
this.pointer = pointer;
this.freeOnGC = freeOnGC;
if (freeOnGC)
GCHandler.enqueuePointer(this);
GCHandler.enqueuePointer(this, pointer);
}

public Pointing(int size, boolean freeOnGC, boolean guard) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.badlogic.gdx.jnigen.runtime.pointer;

import com.badlogic.gdx.jnigen.runtime.CHandler;
import com.badlogic.gdx.jnigen.runtime.gc.GCHandler;

public abstract class StackElementPointer<T extends StackElement> extends Pointing {

Expand Down Expand Up @@ -39,7 +40,10 @@ public T asStackElement() {
public T asStackElement(int index) {
int offset = getSize() * index;
assertBounds(offset);
return createStackElement(getPointer() + offset, getsGCFreed());
T stackElement = createStackElement(getPointer() + offset, false);
if (getsGCFreed())
GCHandler.enqueuePointer(stackElement, getPointer());
return stackElement;
}

public void set(T struct) {
Expand Down

0 comments on commit 8a0bcf6

Please sign in to comment.