Skip to content

Commit a43169f

Browse files
BoxStoreBuilder: do not leak Store when setting as default fails
1 parent 8d0feab commit a43169f

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ For more insights into what changed in the ObjectBox C++ core, [check the Object
99
- When re-creating a `BoxStore` for the same directory and `close()` wasn't called on the previous instance, don't throw
1010
an "Another BoxStore is still open for this directory" exception. Note that calling `close()` *is recommended* before
1111
creating a new instance. [#1201](https://github.com/objectbox/objectbox-java/issues/1201)
12+
- When using `BoxStoreBuilder.buildDefault()`, don't leak Store when setting as default fails.
1213

1314
## 4.3.1 - 2025-08-12
1415

objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,13 @@ static File getDbDir(@Nullable File baseDirectoryOrNull, @Nullable String nameOr
679679
*/
680680
public BoxStore buildDefault() {
681681
BoxStore store = build();
682-
BoxStore.setDefault(store);
682+
try {
683+
BoxStore.setDefault(store);
684+
} catch (IllegalStateException e) {
685+
// Clean up the new store if it can't be set as default
686+
store.close();
687+
throw e;
688+
}
683689
return store;
684690
}
685691

0 commit comments

Comments
 (0)