From 190e4f2e690d1d6316f2c41947dbbe2434ead399 Mon Sep 17 00:00:00 2001 From: luben karavelov Date: Mon, 4 Nov 2019 10:22:02 +0000 Subject: [PATCH] Don't keep ref to the dict if not loaded successfully --- src/main/java/com/github/luben/zstd/ZstdCompressCtx.java | 5 +++-- src/main/java/com/github/luben/zstd/ZstdDecompressCtx.java | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) 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();