Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure disposed memory is removed from Memory#allocatedMemory map #715

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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