diff --git a/CHANGES.md b/CHANGES.md
index 0346b0c3e7..f8fc94aacb 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -10,7 +10,7 @@ Features
Bug Fixes
---------
-
+* [#1447](https://github.com/java-native-access/jna/issues/1447): Null-check cleanable in `c.s.j.Memory#close` - [@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..c83bfed9b5 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
.
@@ -76,7 +77,7 @@ public static void disposeAll() {
}
}
- private Cleaner.Cleanable cleanable;
+ private final Cleaner.Cleanable cleanable;
protected long size; // Size of the malloc'ed space
/** Provide a view into the original memory. Keeps an implicit reference
@@ -123,6 +124,7 @@ public Memory(long size) {
protected Memory() {
super();
+ cleanable = null;
}
/** Provide a view of this memory using the given offset as the base address. The
@@ -180,9 +182,12 @@ public Memory align(int byteBoundary) {
}
/** Free the native memory and set peer to zero */
+ @Override
public void close() {
peer = 0;
- cleanable.clean();
+ if (cleanable != null) {
+ cleanable.clean();
+ }
}
@Deprecated
@@ -783,6 +788,7 @@ public MemoryDisposer(long peer) {
this.peer = peer;
}
+ @Override
public synchronized void run() {
try {
free(peer);