Skip to content

Commit

Permalink
Null-check cleanable in Memory#close
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Jun 28, 2022
1 parent 1eec7dd commit c636e82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Expand Up @@ -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
==============
Expand Down
12 changes: 9 additions & 3 deletions src/com/sun/jna/Memory.java
Expand Up @@ -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;
Expand All @@ -32,6 +31,8 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.sun.jna.internal.Cleaner;

/**
* A <code>Pointer</code> to memory obtained from the native heap via a
* call to <code>malloc</code>.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -783,6 +788,7 @@ public MemoryDisposer(long peer) {
this.peer = peer;
}

@Override
public synchronized void run() {
try {
free(peer);
Expand Down

0 comments on commit c636e82

Please sign in to comment.