https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Andrey Makovskiy

06/12/2019, 8:35 PM
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

Marc Knaup

06/12/2019, 8:48 PM
You can find more info here: https://github.com/JetBrains/kotlin-native/blob/master/CONCURRENCY.md What's your concrete code?
a

Andrey Makovskiy

06/12/2019, 9:00 PM
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

Arkadii Ivanov

06/12/2019, 9:09 PM
Could you please provide more info about what's your class is and how it's created. Some code would be nice.
j

Jurriaan Mous

06/13/2019, 6:14 AM
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

Andrey Makovskiy

06/13/2019, 7:00 AM
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

Jurriaan Mous

06/13/2019, 9:56 AM
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

Andrey Makovskiy

06/13/2019, 12:30 PM
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

Arkadii Ivanov

06/13/2019, 12:34 PM
What is "listener for another thread"? Please provide some piece of code.
m

Marc Knaup

06/13/2019, 12:44 PM
Is
MyClass
a
class
or an
object
? If it is a
class
then where is an instance of it created?
a

Andrey Makovskiy

06/14/2019, 11:35 AM
MyClass
is a class, instance is created in AppDelegate.application() method
m

Marc Knaup

06/14/2019, 1:13 PM
And where is the instance stored?
2 Views