realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.45k stars 1.75k forks source link

Looking for clarification on vector crash #7655

Closed tSquaredd closed 2 years ago

tSquaredd commented 2 years ago

How frequently does the bug occur?

Sometimes

Description

Noticing a weird crash log coming through crashlytics lately. There are 2 different stack traces coming from the same crash site / log on crashlytics. The most common being:

Fatal Exception: java.lang.IllegalStateException: vector
       at io.realm.internal.OsSharedRealm.nativeCommitTransaction(OsSharedRealm.java)
       at io.realm.internal.OsSharedRealm.commitTransaction(OsSharedRealm.java:283)
       at io.realm.BaseRealm.commitTransaction(BaseRealm.java:450)
       at io.realm.Realm.executeTransaction(Realm.java:1594)
       ...

And occasionally:

Fatal Exception: java.lang.OutOfMemoryError: std::bad_alloc in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 144 
       at io.realm.internal.OsSharedRealm.nativeCommitTransaction(OsSharedRealm.java)
       at io.realm.internal.OsSharedRealm.commitTransaction(OsSharedRealm.java:283)
       at io.realm.BaseRealm.commitTransaction(BaseRealm.java:450)
       at io.realm.Realm.executeTransaction(Realm.java:1594)
       ...

I can not share a ton of internal code but the area that it appears to be crashing is during this block on the execute transaction line.

Realm.getDefaultInstance().use { realm ->
    realm.executeTransaction { transactionRealm ->
        layout.updateInRealm(transactionRealm)
    }
}

Mainly wondering what the vector crash could relate to, I have not been able to find much in searching for it. When looking for the out of memory crash I mainly see mentions of not closing realms properly. Recently I started using toFlow for observing changes and I believe I am closing the realms properly but that could be a possible issue. This is how I am doing that:

class SomeRealmRepository {
    val flowRealm: Realm by lazy { Realm.getDefaultInstance() }

    fun getSomeObjects(): Flow<List<SomeObject>?> {
        return flowRealm.where<SomeObject>()
            ... // adding to query
            .findAllAsync()
            .toFlow()
    }

    fun close() {
        flowRealm.close()
    }
}

Then in my viewModel

    override fun onCleared() {
        super.onCleared()
        someRealmRepository.close()
    }

and my view model is scoped to fragment:

private val viewModel: SomeViewModel by viewModels()

I know this is not a ton of information to work with and will be happy to provide additional details if I can.

Stacktrace & log output

No response

Can you reproduce the bug?

Not yet

Reproduction Steps

No response

Version

10.7.1

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

Many Android versions and devices

Build environment

Android Studio version: Android Studio Bumblebee | 2021.1.1 Patch 2 Android Build Tools version: 7.0.3 Gradle version: 7.0.2

bmunkholm commented 2 years ago

@tSquaredd Thanks for reporting this and sorry about the late reply! We are a bit backlogged atm. I would suggest you trace down the OutOfMemory situation, it's likely the root cause. My best suggestion is to stress test your app while monitoring memory usage to see if you are doing anything wrong. Otherwise it may just be related to users using your app on a low-memory device? From our end it's tricky to get much further without more info unfortunately.

edualonso commented 2 years ago

Closing due to inactivity.