Hello for all! Can anybody help with error "kotlin...
# multiplatform
a
Hello for all! Can anybody help with error "kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen". Problem is that object is created in main thread on iOS and exception thrown from constructor. When and why object become frozen?
m
You can find more info here: https://github.com/JetBrains/kotlin-native/blob/master/CONCURRENCY.md What's your concrete code?
a
I already read this article I don't wee why objects become frozen: it is not global variable and song object definition, it references to static object but as I understand it should not make it frozen. They are not objects but classes, instance is created in AppDelegate.application, code is very big, I can't show all. Initially that was framework developed as multiplatform, but tested only in Android.
error trace show that it works in main thread from call AppDelegate.application()
a
Could you please provide more info about what's your class is and how it's created. Some code would be nice.
j
You can use ensureNeverFrozen to see an exception thrown on what freezes the object. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/ensure-never-frozen.html
a
OK, thanks 🙂 I'll try it
mmm... this will not help
function available only from native, when object which frozen is creating inside of common code
@Arkadii Ivanov as I said there is a big amount of code. Too much to publish here and I sure it is not only one place where something will be frozen
I need to understand mechanism and prevent it in all
j
You could make an expect/actual for the function? In the past I found some issues with freezing this way in my common code. I remember mostly being caused by code inside an object or by Lambdas being passed with an unneeded scope which was later frozen.
(Setting scope to Unit for some of those lambdas helped in latter case)
a
Thanks. Already done that but it sill crash with "Mutation attempt of already frozen object MyClass" when MyClass have init { ensureNeverFrozen() }
what if I want to provide a listener for another thread???
a
What is "listener for another thread"? Please provide some piece of code.
m
Is
MyClass
a
class
or an
object
? If it is a
class
then where is an instance of it created?
a
MyClass
is a class, instance is created in AppDelegate.application() method
m
And where is the instance stored?