Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4cc0031

Browse files
CraigacpChangming Sun
authored andcommitted
Java - Fixed a reference counting bug in the OrtEnvironment close method. Added a unit test for the bug.
1 parent 7bb5c35 commit 4cc0031

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

java/src/main/java/ai/onnxruntime/OrtEnvironment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,14 @@ public String toString() {
248248
*/
249249
@Override
250250
public synchronized void close() throws OrtException {
251-
closed = true;
252251
synchronized (refCount) {
253252
int curCount = refCount.get();
254253
if (curCount != 0) {
255254
refCount.decrementAndGet();
256255
}
257256
if (curCount == 1) {
258257
close(OnnxRuntime.ortApiHandle, nativeHandle);
258+
closed = true;
259259
INSTANCE = null;
260260
}
261261
}

java/src/test/java/ai/onnxruntime/InferenceTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1818
import static org.junit.jupiter.api.Assertions.assertEquals;
1919
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
2021
import static org.junit.jupiter.api.Assertions.assertTrue;
2122
import static org.junit.jupiter.api.Assertions.fail;
2223

@@ -70,6 +71,17 @@ public class InferenceTest {
7071
}
7172
}
7273

74+
@Test
75+
public void repeatedCloseTest() throws OrtException {
76+
OrtEnvironment env = OrtEnvironment.getEnvironment("repeatedCloseTest");
77+
try (OrtEnvironment otherEnv = OrtEnvironment.getEnvironment()) {
78+
assertFalse(otherEnv.isClosed());
79+
}
80+
assertFalse(env.isClosed());
81+
env.close();
82+
assertTrue(env.isClosed());
83+
}
84+
7385
@Test
7486
public void createSessionFromPath() throws OrtException {
7587
String modelPath = resourcePath.resolve("squeezenet.onnx").toString();

0 commit comments

Comments
 (0)