diff --git a/CHANGES.md b/CHANGES.md
index 0346b0c3e7..a41fef9108 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -10,7 +10,7 @@ Features
Bug Fixes
---------
-
+* [#1447](https://github.com/java-native-access/jna/issues/1447): Catch possible NPE in `c.s.j.Memory#close` if Cleaner runs before execution - [@dbwiddis](https://github.com/dbwiddis).
Release 5.12.0
==============
diff --git a/src/com/sun/jna/Memory.java b/src/com/sun/jna/Memory.java
index 67d9422902..99aff3aa25 100644
--- a/src/com/sun/jna/Memory.java
+++ b/src/com/sun/jna/Memory.java
@@ -22,7 +22,6 @@
*/
package com.sun.jna;
-import com.sun.jna.internal.Cleaner;
import java.io.Closeable;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
@@ -32,6 +31,8 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import com.sun.jna.internal.Cleaner;
+
/**
* A Pointer
to memory obtained from the native heap via a
* call to malloc
.
@@ -180,9 +181,14 @@ public Memory align(int byteBoundary) {
}
/** Free the native memory and set peer to zero */
+ @Override
public void close() {
peer = 0;
- cleanable.clean();
+ try {
+ cleanable.clean();
+ } catch (NullPointerException npe) {
+ // If Cleaner has removed the reference it is null, ignore
+ }
}
@Deprecated
@@ -783,6 +789,7 @@ public MemoryDisposer(long peer) {
this.peer = peer;
}
+ @Override
public synchronized void run() {
try {
free(peer);