Skip to content

Commit

Permalink
Catch possible NPE in Memory#close if Cleaner runs first
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Jun 28, 2022
1 parent 1eec7dd commit c12e403
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 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): Catch possible NPE in `c.s.j.Memory#close` if Cleaner runs before execution - [@dbwiddis](https://github.com/dbwiddis).

Release 5.12.0
==============
Expand Down
11 changes: 9 additions & 2 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 @@ -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
Expand Down Expand Up @@ -783,6 +789,7 @@ public MemoryDisposer(long peer) {
this.peer = peer;
}

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

0 comments on commit c12e403

Please sign in to comment.