Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/main/java/io/github/metarank/lightgbm4j/LGBMBooster.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class LGBMBooster implements AutoCloseable {

private static volatile boolean nativeLoaded = false;

private volatile boolean isClosed = false;

static {
try {
LGBMBooster.loadNative();
Expand Down Expand Up @@ -171,9 +173,12 @@ public static LGBMBooster loadModelFromString(String model) throws LGBMException
*/
@Override
public void close() throws LGBMException {
int result = LGBM_BoosterFree(voidpp_value(handle));
if (result < 0) {
throw new LGBMException(LGBM_GetLastError());
if (!isClosed) {
isClosed = true;
int result = LGBM_BoosterFree(voidpp_value(handle));
if (result < 0) {
throw new LGBMException(LGBM_GetLastError());
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/io/github/metarank/lightgbm4j/LGBMDataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static com.microsoft.ml.lightgbm.lightgbmlib.*;

public class LGBMDataset implements AutoCloseable {
private volatile boolean isClosed = false;
public SWIGTYPE_p_void handle;
static {
try {
Expand Down Expand Up @@ -242,9 +243,12 @@ public String[] getFeatureNames() throws LGBMException {
*/
@Override
public void close() throws LGBMException {
int result = LGBM_DatasetFree(handle);
if (result < 0) {
throw new LGBMException(LGBM_GetLastError());
if (!isClosed) {
int result = LGBM_DatasetFree(handle);
isClosed = true;
if (result < 0) {
throw new LGBMException(LGBM_GetLastError());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,12 @@ void testCreateByReference() throws LGBMException {
assertEquals(train[0], test[0], 0.001);
}

@Test void testDoubleClose() throws LGBMException {
LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
LGBMBooster booster = LGBMBooster.create(ds, "");
booster.close();
booster.close();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ public void testCreateFromFileFail() {
ds.close();
}

@Test void testDoubleClose() throws LGBMException {
LGBMDataset ds = LGBMDataset.createFromMat(new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 2, 2, true, "", null);
ds.close();
ds.close();
}
}