Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread #799

Closed
XandrMaster opened this issue Apr 22, 2022 · 4 comments · Fixed by #822
Assignees

Comments

@XandrMaster
Copy link

XandrMaster commented Apr 22, 2022

Hello guys . I have an exception and crash only for iOs when I config my Real from non-main thread 🙁 . Take a look pls to source code:

@ThreadLocal
object RealmManager {

    val realmDb by lazy { initRealmAsync() }

    private fun initRealmAsync(): Deferred<Realm> {
        return CoroutineScope(Dispatchers.Default).async {
            val config = RealmConfiguration.Builder(
                schema = setOf(
                    UserEntity::class,
                    EquipmentEntity::class,
                    UserActionEntity::class))
                .log(LogLevel.ALL)
                .schemaVersion(1)
                .build()
            Realm.open(config)
        }
    }

    fun forceInit() {
        CoroutineScope(Dispatchers.Default).launch {
            realmDb.await()
        }
    }
}

if I calling forceInit() from iOs it crashes . This code in common module . For Android works fine .
With Dispatchers.Main all good.
Thanks .

@cmelchior
Copy link
Contributor

Hi @XandrMaster Can you provide a bit more information:

  • What code do you run to see this error?
  • What version of the Realm Kotlin SDK?
  • Version of coroutines?
  • Version of Kotlin?

Also, any reason you are making the RealmManager ThreadLocal? The Kotlin SDK no longer requires the Realm instance to to locked to a specific thread, so you can just open a single global instance.

@XandrMaster
Copy link
Author

Sure :

  • just run from Application :
class App : Application() {

    override fun onCreate() {
        super.onCreate()

        RealmManager.forceInit()

        Log.d("App", " DB Init ")
    }
}
 implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-native-mt")
      implementation("io.realm.kotlin:library-base:0.10.0")
  • 211-1.6.21-release-334-AS7442.40

@rorbech
Copy link
Contributor

rorbech commented Apr 25, 2022

@XandrMaster Could you also please supply the stack trace from the error to indicate which top level value that is causing the issue?

@cmelchior cmelchior self-assigned this Apr 25, 2022
@XandrMaster
Copy link
Author

@XandrMaster Could you also please supply the stack trace from the error to indicate which top level value that is causing the issue?

Sure , here you are:

Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread
    at 0   RealmKmmTest                        0x1071330a8        kfun:kotlin.Throwable#<init>(kotlin.String?){} + 88 
    at 1   RealmKmmTest                        0x10712cfb6        kfun:kotlin.Exception#<init>(kotlin.String?){} + 86 
    at 2   RealmKmmTest                        0x10712d1a6        kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 86 
    at 3   RealmKmmTest                        0x10713ff66        kfun:kotlin.native.IncorrectDereferenceException#<init>(kotlin.String){} + 86 
    at 4   RealmKmmTest                        0x10715d048        ThrowIncorrectDereferenceException + 120 
    at 5   RealmKmmTest                        0x1072680fd        CheckGlobalsAccessible + 29 
    at 6   RealmKmmTest                        0x10737ecbb        kfun:io.realm.internal.interop#<get-INVALID_CLASS_KEY>(){}io.realm.internal.interop.ClassKey + 123 
    at 7   RealmKmmTest                        0x107373a89        kfun:io.realm.internal.interop.ClassInfo#<init>(kotlin.String;kotlin.String?;kotlin.Long;kotlin.Long;io.realm.internal.interop.ClassKey?;kotlin.Int;kotlin.Int;kotlin.native.internal.DefaultConstructorMarker?){} + 617 
    at 8   RealmKmmTest                        0x107373fb3        kfun:io.realm.internal.interop.ClassInfo.Companion#create(kotlin.String;kotlin.String?;kotlin.Long){}io.realm.internal.interop.ClassInfo + 451 
    at 9   RealmKmmTest                        0x1070cd772        kfun:com.example.kmmrealmtestapp.data.db.UserEntity.Companion#$realm$schema(){}kotlin.Any + 546 
    at 10  RealmKmmTest                        0x10783ac1a        kfun:io.realm.internal.ConfigurationImpl#<init>(kotlin.String?;kotlin.String;kotlin.collections.Set<kotlin.reflect.KClass<out|io.realm.RealmObject>>;io.realm.LogConfiguration;kotlin.Long;kotlinx.coroutines.CoroutineDispatcher;kotlinx.coroutines.CoroutineDispatcher;kotlin.Long;io.realm.internal.interop.SchemaMode;kotlin.ByteArray?;io.realm.CompactOnLaunchCallback?;io.realm.migration.RealmMigration?){} + 4218 
    at 11  RealmKmmTest                        0x107842ea6        kfun:io.realm.internal.RealmConfigurationImpl#<init>(kotlin.String?;kotlin.String;kotlin.collections.Set<kotlin.reflect.KClass<out|io.realm.RealmObject>>;io.realm.LogConfiguration;kotlin.Long;kotlinx.coroutines.CoroutineDispatcher;kotlinx.coroutines.CoroutineDispatcher;kotlin.Long;kotlin.ByteArray?;kotlin.Boolean;io.realm.CompactOnLaunchCallback?;io.realm.migration.RealmMigration?){} + 902 
    at 12  RealmKmmTest                        0x107835cf6        kfun:io.realm.RealmConfiguration.Builder#build(){}io.realm.RealmConfiguration + 1478 
    at 13  RealmKmmTest                        0x1070cb889        kfun:com.example.kmmrealmtestapp.data.db.RealmManager.$initRealmAsync$lambda-1COROUTINE$0.invokeSuspend#internal + 761 
    at 14  RealmKmmTest                        0x107136bdd        kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 685 
    at 15  RealmKmmTest                        0x10733105e        kfun:kotlinx.coroutines.DispatchedTask#run(){} + 3182 
    at 16  RealmKmmTest                        0x1072ba74a        kfun:kotlinx.coroutines.EventLoopImplBase#processNextEvent(){}kotlin.Long + 826 
    at 17  RealmKmmTest                        0x10735a5a7        kfun:kotlinx.coroutines#runEventLoop(kotlinx.coroutines.EventLoop?;kotlin.Function0<kotlin.Boolean>){} + 887 
    at 18  RealmKmmTest                        0x107362231        kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.start$lambda-0#internal + 385 
    at 19  RealmKmmTest                        0x10736236c        kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$73.invoke#internal + 60 
    at 20  RealmKmmTest                        0x10736244c        kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$73.$<bridge-UNN>invoke(){}#internal + 60 
    at 21  RealmKmmTest                        0x107142d1a        WorkerLaunchpad + 186 
    at 22  RealmKmmTest                        0x1072839a6        _ZN6Worker19processQueueElementEb + 2278 
    at 23  RealmKmmTest                        0x10728307a        _ZN12_GLOBAL__N_113workerRoutineEPv + 74 
    at 24  libsystem_pthread.dylib             0x7fff6da32513     _pthread_start + 124 
    at 25  libsystem_pthread.dylib             0x7fff6da2e02e     thread_start + 14 

Process finished with exit code 0

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants