Merge pull request #626 from JavaSaBr/fix_memory_leak_lwjgl
Fixed some problems with getting an address of an LWJGL buffer.
This commit is contained in:
commit
b35cbd5580
@ -4,10 +4,10 @@ import org.lwjgl.system.MemoryUtil;
|
|||||||
|
|
||||||
import java.lang.ref.PhantomReference;
|
import java.lang.ref.PhantomReference;
|
||||||
import java.lang.ref.ReferenceQueue;
|
import java.lang.ref.ReferenceQueue;
|
||||||
import java.nio.Buffer;
|
import java.nio.*;
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The implementation of the {@link BufferAllocator} which use {@link MemoryUtil} to manage memory.
|
* The implementation of the {@link BufferAllocator} which use {@link MemoryUtil} to manage memory.
|
||||||
@ -16,6 +16,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
*/
|
*/
|
||||||
public class LWJGLBufferAllocator implements BufferAllocator {
|
public class LWJGLBufferAllocator implements BufferAllocator {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(LWJGLBufferAllocator.class.getName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The reference queue.
|
* The reference queue.
|
||||||
*/
|
*/
|
||||||
@ -31,8 +33,7 @@ public class LWJGLBufferAllocator implements BufferAllocator {
|
|||||||
*/
|
*/
|
||||||
private volatile Long address;
|
private volatile Long address;
|
||||||
|
|
||||||
public Deallocator(final ByteBuffer referent, final ReferenceQueue<? super ByteBuffer> queue,
|
Deallocator(final ByteBuffer referent, final ReferenceQueue<? super ByteBuffer> queue, final Long address) {
|
||||||
final Long address) {
|
|
||||||
super(referent, queue);
|
super(referent, queue);
|
||||||
this.address = address;
|
this.address = address;
|
||||||
}
|
}
|
||||||
@ -75,7 +76,7 @@ public class LWJGLBufferAllocator implements BufferAllocator {
|
|||||||
*/
|
*/
|
||||||
private static void freeByteBuffers() {
|
private static void freeByteBuffers() {
|
||||||
try {
|
try {
|
||||||
for (;;) {
|
for (; ; ) {
|
||||||
final Deallocator deallocator = (Deallocator) DUMMY_QUEUE.remove();
|
final Deallocator deallocator = (Deallocator) DUMMY_QUEUE.remove();
|
||||||
deallocator.free();
|
deallocator.free();
|
||||||
}
|
}
|
||||||
@ -87,15 +88,53 @@ public class LWJGLBufferAllocator implements BufferAllocator {
|
|||||||
@Override
|
@Override
|
||||||
public void destroyDirectBuffer(final Buffer buffer) {
|
public void destroyDirectBuffer(final Buffer buffer) {
|
||||||
|
|
||||||
final long address = MemoryUtil.memAddress((ByteBuffer) buffer);
|
final long address = getAddress(buffer);
|
||||||
|
|
||||||
|
if (address == -1) {
|
||||||
|
LOGGER.warning("Not found address of the " + buffer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// disable deallocator
|
// disable deallocator
|
||||||
final Deallocator deallocator = DEALLOCATORS.remove(address);
|
final Deallocator deallocator = DEALLOCATORS.remove(address);
|
||||||
if (deallocator != null) deallocator.setAddress(null);
|
|
||||||
|
if (deallocator == null) {
|
||||||
|
LOGGER.warning("Not found a deallocator for address " + address);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
deallocator.setAddress(null);
|
||||||
|
|
||||||
MemoryUtil.memFree(buffer);
|
MemoryUtil.memFree(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get memory address of the buffer.
|
||||||
|
*
|
||||||
|
* @param buffer the buffer.
|
||||||
|
* @return the address or -1.
|
||||||
|
*/
|
||||||
|
private long getAddress(final Buffer buffer) {
|
||||||
|
|
||||||
|
if (buffer instanceof ByteBuffer) {
|
||||||
|
return MemoryUtil.memAddress((ByteBuffer) buffer, 0);
|
||||||
|
} else if (buffer instanceof ShortBuffer) {
|
||||||
|
return MemoryUtil.memAddress((ShortBuffer) buffer, 0);
|
||||||
|
} else if (buffer instanceof CharBuffer) {
|
||||||
|
return MemoryUtil.memAddress((CharBuffer) buffer, 0);
|
||||||
|
} else if (buffer instanceof IntBuffer) {
|
||||||
|
return MemoryUtil.memAddress((IntBuffer) buffer, 0);
|
||||||
|
} else if (buffer instanceof FloatBuffer) {
|
||||||
|
return MemoryUtil.memAddress((FloatBuffer) buffer, 0);
|
||||||
|
} else if (buffer instanceof LongBuffer) {
|
||||||
|
return MemoryUtil.memAddress((LongBuffer) buffer, 0);
|
||||||
|
} else if (buffer instanceof DoubleBuffer) {
|
||||||
|
return MemoryUtil.memAddress((DoubleBuffer) buffer, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuffer allocate(final int size) {
|
public ByteBuffer allocate(final int size) {
|
||||||
final Long address = MemoryUtil.nmemAlloc(size);
|
final Long address = MemoryUtil.nmemAlloc(size);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user