Skip to content

Commit 67e6e4b

Browse files
authored
Fix getLocalInstanceCount crashing when Realms are closed again. (#3798)
1 parent 101b5b4 commit 67e6e4b

3 files changed

Lines changed: 8 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Bug fixes
88

99
* Added version number to the native library, preventing ReLinker from accidentally loading old code (#3775).
10+
* `Realm.getLocalInstanceCount(config)` throwing NullPointerException if called after all Realms have been closed (#3791).
1011

1112
## 2.2.0
1213

realm/realm-library/src/androidTest/java/io/realm/RealmTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3796,20 +3796,20 @@ public void run() {
37963796
@Test
37973797
public void getLocalInstanceCount() {
37983798
final RealmConfiguration config = configFactory.createConfiguration("localInstanceCount");
3799-
assertEquals(0, Realm.getGlobalInstanceCount(config));
3799+
assertEquals(0, Realm.getLocalInstanceCount(config));
38003800

38013801
// Open thread local Realm
38023802
Realm realm = Realm.getInstance(config);
3803-
assertEquals(1, Realm.getGlobalInstanceCount(config));
3803+
assertEquals(1, Realm.getLocalInstanceCount(config));
38043804

38053805
// Open thread local DynamicRealm
38063806
DynamicRealm dynRealm = DynamicRealm.getInstance(config);
3807-
assertEquals(2, Realm.getGlobalInstanceCount(config));
3807+
assertEquals(2, Realm.getLocalInstanceCount(config));
38083808

38093809
dynRealm.close();
3810-
assertEquals(1, Realm.getGlobalInstanceCount(config));
3810+
assertEquals(1, Realm.getLocalInstanceCount(config));
38113811
realm.close();
3812-
assertEquals(0, Realm.getGlobalInstanceCount(config));
3812+
assertEquals(0, Realm.getLocalInstanceCount(config));
38133813
}
38143814

38153815
@Test

realm/realm-library/src/main/java/io/realm/RealmCache.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ static int getLocalThreadCount(RealmConfiguration configuration) {
402402
} else {
403403
int totalRefCount = 0;
404404
for (RealmCacheType type : RealmCacheType.values()) {
405-
totalRefCount += cache.refAndCountMap.get(type).localCount.get();
405+
Integer localCount = cache.refAndCountMap.get(type).localCount.get();
406+
totalRefCount += (localCount != null) ? localCount : 0;
406407
}
407408
return totalRefCount;
408409
}

0 commit comments

Comments
 (0)