Skip to content

Commit

Permalink
Fix missing Native free of Dict: Even when using a by-reference buffe…
Browse files Browse the repository at this point in the history
…r, the dict struct itself is malloc'ed and thus needs freeing to not leak memory.
  • Loading branch information
Morten Grouleff authored and luben committed Apr 23, 2024
1 parent 1ff8933 commit 2d33a1e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
9 changes: 3 additions & 6 deletions src/main/java/com/github/luben/zstd/ZstdDictCompress.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public ZstdDictCompress(ByteBuffer dict, int level, boolean byReference) {
throw new IllegalStateException("ZSTD_createCDict failed");
}
if (byReference) {
sharedDict = dict; // ensures the dict is not garbage collected while this object remains, and flags that we should not use native free.
sharedDict = dict; // ensures the dict is not garbage collected while this object remains.
}
// Ensures that even if ZstdDictCompress is created and published through a race, no thread could observe
// nativePtr == 0.
Expand All @@ -110,12 +110,9 @@ int level() {
@Override
void doClose() {
if (nativePtr != 0) {
if (sharedDict == null) {
free();
} else {
sharedDict = null;
}
free();
nativePtr = 0;
sharedDict = null;
}
}
}
9 changes: 3 additions & 6 deletions src/main/java/com/github/luben/zstd/ZstdDictDecompress.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public ZstdDictDecompress(ByteBuffer dict, boolean byReference) {
throw new IllegalStateException("ZSTD_createDDict failed");
}
if (byReference) {
sharedDict = dict; // ensures the dict is not garbage collected while this object remains, and flags that we should not use native free.
sharedDict = dict; // ensures the dict is not garbage collected while this object remains
}
// Ensures that even if ZstdDictDecompress is created and published through a race, no thread could observe
// nativePtr == 0.
Expand All @@ -97,12 +97,9 @@ public ZstdDictDecompress(ByteBuffer dict, boolean byReference) {
@Override
void doClose() {
if (nativePtr != 0) {
if (sharedDict == null) {
free();
} else {
sharedDict = null;
}
free();
nativePtr = 0;
sharedDict = null;
}
}
}

0 comments on commit 2d33a1e

Please sign in to comment.