【问题标题】:kotlin-native freeze() method crashing when freezing a functionkotlin-native freeze() 方法在冻结函数时崩溃
【发布时间】:2019-10-15 07:28:01
【问题描述】:

我正在尝试冻结一个能够传递给工作人员的函数。在与函数相同的类中,我有一个标有ensureNeverFrozen 的变量。这个变量没有在我试图冻结的函数中使用,事实上,该函数没有做任何事情。

不过,当我尝试冻结函数时,出现以下错误

最小的崩溃重现code -

Instances of kotlin.Error, kotlin.RuntimeException and subclasses aren't propagated from Kotlin to Objective-C/Swift.
Other exceptions can be propagated as NSError if method has or inherits @Throws annotation.
Uncaught Kotlin exception: kotlin.native.concurrent.FreezingException: freezing of function performWork (Kotlin reflection is not available) has failed, first blocker is []
        at 0   lib                                 0x0000000102502a45 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 85
        at 1   lib                                 0x0000000102502125 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 85
        at 2   lib                                 0x00000001025265c1 kfun:kotlin.native.concurrent.FreezingException.<init>(kotlin.Any;kotlin.Any)kotlin.native.concurrent.FreezingException + 641
        at 3   lib                                 0x0000000102526a2e ThrowFreezingException + 222
        at 4   lib                                 0x0000000102575833 FreezeSubgraph + 2611
        at 5   lib                                 0x000000010258bcfb Kotlin_Worker_freezeInternal + 27
        at 6   lib                                 0x000000010252673b kfun:kotlin.native.concurrent.freeze@#GENERIC.()Generic + 59
        at 7   lib                                 0x000000010253e310 kfun:com.prateekgrover.lib.KNDispatchQueue.performWorkAsync() + 208
        at 8   lib                                 0x000000010253de1a kfun:com.prateekgrover.lib.Greeting.start() + 186
        at 9   lib                                 0x000000010254168d blockCopyHelper + 573
        at 10  ios_kn_sample                       0x000000010215471d $sIeg_IeyB_TR + 45 (/Users/prateek.grover/Documents/code/KotlinNative/KotlinNativeSample/core_lib_dup/ios_kn_sample/<compiler-generated>:<unknown>)
        at 11  libdispatch.dylib                   0x0000000104156d7f _dispatch_call_block_and_release + 12
        at 12  libdispatch.dylib                   0x0000000104157db5 _dispatch_client_callout + 8
        at 13  libdispatch.dylib                   0x000000010415a7b9 _dispatch_queue_override_invoke + 1022
        at 14  libdispatch.dylib                   0x0000000104168632 _dispatch_root_queue_drain + 351
        at 15  libdispatch.dylib                   0x0000000104168fca _dispatch_worker_thread2 + 130
        at 16  libsystem_pthread.dylib             0x00000001045406b3 _pthread_wqthread + 583
        at 17  libsystem_pthread.dylib             0x00000001045403fd start_wqthread + 13

对上述行为有何解释?

【问题讨论】:

    标签: ios kotlin worker kotlin-multiplatform kotlin-native


    【解决方案1】:

    我假设你指的是这个

    actual class KNDispatchQueue {
        @ThreadLocal
        actual companion object Singleton {
            actual val sharedInstance: KNDispatchQueue = KNDispatchQueue()
        }
    
        private var workQueue: MutableList<KNWork> = mutableListOf()
    
        init {
            workQueue.ensureNeverFrozen()
        }
    
        actual fun performWorkAsync() {
            ::performWork.freeze()
        }
    
        fun performWork() {
    
        }
    }
    

    函数是类的一部分。假设它试图冻结它自己的那个实例,它也会冻结图形的其余部分。我注释掉了ensureNeverFrozen,它冻结了。

    您的语法 ::performWork 指向实例引用。

    https://kotlinlang.org/docs/reference/reflection.html#bound-function-and-property-references-since-11

    【讨论】:

    • 所以基本上因为 ::performWork 绑定到它的对象(KNDispatchQueue 的实例),它试图释放整个实例?
    • 是的。 Freeze 抓取与实例相关的任何内容并将其冻结。但是,错误消息不是很好。它们通常更清晰一些。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    相关资源
    最近更新 更多