Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null-check cleanable in Memory#close #1447

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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