diff --git a/src/main/java/com/github/luben/zstd/ZstdCompressCtx.java b/src/main/java/com/github/luben/zstd/ZstdCompressCtx.java index fae39326..a4556189 100644 --- a/src/main/java/com/github/luben/zstd/ZstdCompressCtx.java +++ b/src/main/java/com/github/luben/zstd/ZstdCompressCtx.java @@ -103,8 +103,7 @@ public void loadDict(ZstdDictCompress dict) { if (nativePtr == 0) { throw new IllegalStateException("Compression context is closed"); } - // keep a reference to the ditionary so it's not garbage collected - compression_dict = dict; + acquireSharedLock(); dict.acquireSharedLock(); try { @@ -112,6 +111,8 @@ public void loadDict(ZstdDictCompress dict) { if (Zstd.isError(result)) { throw new ZstdException(result); } + // keep a reference to the dictionary so it's not garbage collected + compression_dict = dict; } finally { dict.releaseSharedLock(); releaseSharedLock(); diff --git a/src/main/java/com/github/luben/zstd/ZstdDecompressCtx.java b/src/main/java/com/github/luben/zstd/ZstdDecompressCtx.java index 46da79ff..fc040947 100644 --- a/src/main/java/com/github/luben/zstd/ZstdDecompressCtx.java +++ b/src/main/java/com/github/luben/zstd/ZstdDecompressCtx.java @@ -47,8 +47,6 @@ public void loadDict(ZstdDictDecompress dict) { if (nativePtr == 0) { throw new IllegalStateException("Decompression context is closed"); } - // keep a reference to the ditionary so it's not garbage collected - decompression_dict = dict; acquireSharedLock(); dict.acquireSharedLock(); try { @@ -56,6 +54,8 @@ public void loadDict(ZstdDictDecompress dict) { if (Zstd.isError(result)) { throw new ZstdException(result); } + // keep a reference to the dictionary so it's not garbage collected + decompression_dict = dict; } finally { dict.releaseSharedLock(); releaseSharedLock();