Skip to content

Commit 2d33a1e

Browse files
Morten Grouleffluben
Morten Grouleff
authored andcommitted
Fix missing Native free of Dict: Even when using a by-reference buffer, the dict struct itself is malloc'ed and thus needs freeing to not leak memory.
1 parent 1ff8933 commit 2d33a1e

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

src/main/java/com/github/luben/zstd/ZstdDictCompress.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public ZstdDictCompress(ByteBuffer dict, int level, boolean byReference) {
9595
throw new IllegalStateException("ZSTD_createCDict failed");
9696
}
9797
if (byReference) {
98-
sharedDict = dict; // ensures the dict is not garbage collected while this object remains, and flags that we should not use native free.
98+
sharedDict = dict; // ensures the dict is not garbage collected while this object remains.
9999
}
100100
// Ensures that even if ZstdDictCompress is created and published through a race, no thread could observe
101101
// nativePtr == 0.
@@ -110,12 +110,9 @@ int level() {
110110
@Override
111111
void doClose() {
112112
if (nativePtr != 0) {
113-
if (sharedDict == null) {
114-
free();
115-
} else {
116-
sharedDict = null;
117-
}
113+
free();
118114
nativePtr = 0;
115+
sharedDict = null;
119116
}
120117
}
121118
}

src/main/java/com/github/luben/zstd/ZstdDictDecompress.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public ZstdDictDecompress(ByteBuffer dict, boolean byReference) {
8686
throw new IllegalStateException("ZSTD_createDDict failed");
8787
}
8888
if (byReference) {
89-
sharedDict = dict; // ensures the dict is not garbage collected while this object remains, and flags that we should not use native free.
89+
sharedDict = dict; // ensures the dict is not garbage collected while this object remains
9090
}
9191
// Ensures that even if ZstdDictDecompress is created and published through a race, no thread could observe
9292
// nativePtr == 0.
@@ -97,12 +97,9 @@ public ZstdDictDecompress(ByteBuffer dict, boolean byReference) {
9797
@Override
9898
void doClose() {
9999
if (nativePtr != 0) {
100-
if (sharedDict == null) {
101-
free();
102-
} else {
103-
sharedDict = null;
104-
}
100+
free();
105101
nativePtr = 0;
102+
sharedDict = null;
106103
}
107104
}
108105
}

0 commit comments

Comments
 (0)