Skip to content

Commit 0ff5744

Browse files
committed
Catch possible NPE in Memory#close if Cleaner runs first
1 parent 1eec7dd commit 0ff5744

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Features
1010

1111
Bug Fixes
1212
---------
13-
13+
* [#1447](https://github.com/java-native-access/jna/issues/1447): Catch possible NPE in `c.s.j.Memory#close` if Cleaner runs first - [@dbwiddis](https://github.com/dbwiddis).
1414

1515
Release 5.12.0
1616
==============

src/com/sun/jna/Memory.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
*/
2323
package com.sun.jna;
2424

25-
import com.sun.jna.internal.Cleaner;
2625
import java.io.Closeable;
2726
import java.lang.ref.Reference;
2827
import java.lang.ref.WeakReference;
@@ -32,6 +31,8 @@
3231
import java.util.Map;
3332
import java.util.concurrent.ConcurrentHashMap;
3433

34+
import com.sun.jna.internal.Cleaner;
35+
3536
/**
3637
* A <code>Pointer</code> to memory obtained from the native heap via a
3738
* call to <code>malloc</code>.
@@ -180,9 +181,14 @@ public Memory align(int byteBoundary) {
180181
}
181182

182183
/** Free the native memory and set peer to zero */
184+
@Override
183185
public void close() {
184186
peer = 0;
185-
cleanable.clean();
187+
try {
188+
cleanable.clean();
189+
} catch (NullPointerException npe) {
190+
// If Cleaner has removed the reference it is null, ignore
191+
}
186192
}
187193

188194
@Deprecated
@@ -783,6 +789,7 @@ public MemoryDisposer(long peer) {
783789
this.peer = peer;
784790
}
785791

792+
@Override
786793
public synchronized void run() {
787794
try {
788795
free(peer);

0 commit comments

Comments
 (0)