Skip to content
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
17 changes: 17 additions & 0 deletions netcdf4/src/main/java/ucar/nc2/ffi/netcdf/NetcdfClibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package ucar.nc2.ffi.netcdf;

import com.google.common.base.Strings;
import com.sun.jna.Library;
import com.sun.jna.Native;
import java.lang.reflect.Proxy;
import javax.annotation.Nullable;
import ucar.nc2.jni.netcdf.Nc4prototypes;
import ucar.nc2.jni.netcdf.Nc4wrapper;
Expand Down Expand Up @@ -128,6 +130,21 @@ public static synchronized int setLogLevel(int level) {
return oldlevel;
}

/**
* Closes the netCDF-C native library.
*
* <p>
* Call this method to ensure JNA can shut down its cleaner thread; otherwise JVM shutdown may be blocked.
* </p>
*/
public static synchronized void shutdown() {
if (nc4 != null) {
Library.Handler lh = (Library.Handler) Proxy.getInvocationHandler(nc4);
lh.getNativeLibrary().close();
nc4 = null;
}
}

private static Nc4prototypes load() {
if (nc4 == null) {
if (jnaPath == null) {
Expand Down
5 changes: 5 additions & 0 deletions uicdm/src/main/java/ucar/nc2/ui/ToolsUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import ucar.nc2.dods.DODSNetcdfFile;
import ucar.nc2.dt.GridDataset;
import ucar.nc2.dt.RadialDatasetSweep;
import ucar.nc2.ffi.netcdf.NetcdfClibrary;
import ucar.nc2.ft.point.PointDatasetImpl;
import ucar.nc2.ft2.coverage.*;
import ucar.nc2.grib.GribIndexCache;
Expand Down Expand Up @@ -1312,6 +1313,10 @@ public void run(Object o) {

public static void exit() {
doSavePrefsAndUI();
// close netCDF-C library, if loaded
if (NetcdfClibrary.isLibraryPresent()) {
NetcdfClibrary.shutdown();
}
System.exit(0);
}

Expand Down