Skip to content

Commit

Permalink
Ensure disposed memory is removed from Memory#allocatedMemory map
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasblaesing committed Oct 10, 2016
1 parent 4160d35 commit b31240f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Bug Fixes
* [#696](https://github.com/java-native-access/jna/issues/696): COMLateBindingObject.getAutomationProperty method that takes iDispatch parameter doesn't use it - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#664](https://github.com/java-native-access/jna/issues/664): Prevent premature GC of Pointer and Function objects by passing whole object into JNI call in addition to the raw pointer value - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#669](https://github.com/java-native-access/jna/pull/669): Ensure XSI-compliant strerror_r is used, to prevent corrupted error messages on linux - [@DavidKeller](https://github.com/DavidKeller).
* [#697](https://github.com/java-native-access/jna/issues/697): Ensure disposed memory is removed from Memory#allocatedMemory map - [@matthiasblaesing](https://github.com/matthiasblaesing).

Release 4.2.1
=============
Expand Down
2 changes: 1 addition & 1 deletion src/com/sun/jna/Memory.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ protected synchronized void dispose() {
try {
free(peer);
} finally {
peer = 0;
allocatedMemory.remove(this);
peer = 0;
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/com/sun/jna/MemoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.Map;

import junit.framework.TestCase;

Expand Down Expand Up @@ -175,6 +177,25 @@ public void testDump() {
"[08090a0b]" + ls +
"[0c0d0e]" + ls, m.dump());
}

public void testRemoveAllocatedMemoryMap() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
// Make sure there are no remaining allocations
Memory.disposeAll();

// get a reference to the allocated memory
Field allocatedMemoryField = Memory.class.getDeclaredField("allocatedMemory");
allocatedMemoryField.setAccessible(true);
Map<Memory, Reference<Memory>> allocatedMemory = (Map<Memory, Reference<Memory>>) allocatedMemoryField.get(null);
assertEquals(0, allocatedMemory.size());

// Test allocation and ensure it is accounted for
Memory mem = new Memory(1024);
assertEquals(1, allocatedMemory.size());

// Dispose memory and ensure allocation is removed from allocatedMemory-Map
mem.dispose();
assertEquals(0, allocatedMemory.size());
}

public static void main(String[] args) {
junit.textui.TestRunner.run(MemoryTest.class);
Expand Down

0 comments on commit b31240f

Please sign in to comment.