Skip to content

Commit 84b63c1

Browse files
authored
Merge pull request #1521 from lesserwhirls/ncc
Add method to close netCDF-C native library
2 parents 4b92579 + 6211f36 commit 84b63c1

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

netcdf4/src/main/java/ucar/nc2/ffi/netcdf/NetcdfClibrary.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
package ucar.nc2.ffi.netcdf;
77

88
import com.google.common.base.Strings;
9+
import com.sun.jna.Library;
910
import com.sun.jna.Native;
11+
import java.lang.reflect.Proxy;
1012
import javax.annotation.Nullable;
1113
import ucar.nc2.jni.netcdf.Nc4prototypes;
1214
import ucar.nc2.jni.netcdf.Nc4wrapper;
@@ -128,6 +130,21 @@ public static synchronized int setLogLevel(int level) {
128130
return oldlevel;
129131
}
130132

133+
/**
134+
* Closes the netCDF-C native library.
135+
*
136+
* <p>
137+
* Call this method to ensure JNA can shut down its cleaner thread; otherwise JVM shutdown may be blocked.
138+
* </p>
139+
*/
140+
public static synchronized void shutdown() {
141+
if (nc4 != null) {
142+
Library.Handler lh = (Library.Handler) Proxy.getInvocationHandler(nc4);
143+
lh.getNativeLibrary().close();
144+
nc4 = null;
145+
}
146+
}
147+
131148
private static Nc4prototypes load() {
132149
if (nc4 == null) {
133150
if (jnaPath == null) {

uicdm/src/main/java/ucar/nc2/ui/ToolsUI.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import ucar.nc2.dods.DODSNetcdfFile;
1717
import ucar.nc2.dt.GridDataset;
1818
import ucar.nc2.dt.RadialDatasetSweep;
19+
import ucar.nc2.ffi.netcdf.NetcdfClibrary;
1920
import ucar.nc2.ft.point.PointDatasetImpl;
2021
import ucar.nc2.ft2.coverage.*;
2122
import ucar.nc2.grib.GribIndexCache;
@@ -1312,6 +1313,10 @@ public void run(Object o) {
13121313

13131314
public static void exit() {
13141315
doSavePrefsAndUI();
1316+
// close netCDF-C library, if loaded
1317+
if (NetcdfClibrary.isLibraryPresent()) {
1318+
NetcdfClibrary.shutdown();
1319+
}
13151320
System.exit(0);
13161321
}
13171322

0 commit comments

Comments
 (0)