darkmoon_uk
03/17/2021, 7:15 AMCaused by: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap@33944e8
...while adding an element to a MutableSet
. However:
• The Set is created while on the main thread
• The Set is only ever accessed from the main thread (I've asserted these points with checks on NSThread.isMainThread
at every relevant point)
• The top level object under which the Set (indirectly) resides, is marked @ThreadLocal
...which other restriction am I missing!?
Is there a way to determine at what point an object is being frozen?Tijl
03/17/2021, 7:19 AMensureNeverFrozen
after creating it, and then get an exception when something tries to freeze itdarkmoon_uk
03/17/2021, 7:19 AMobject
that wasn't @ThreadLocal
.object
will be frozen unless marked @ThreadLocal
.
Would it be reasonable for the docs to advise that applying @ThreadLocal
should be considered a sensible 'default' for newcomers.russhwolf
03/17/2021, 12:48 PM@ThreadLocal
by default would have it's own set of confusion and drawbacks. Anytime you access it from a background thread, none of the state you've set in the main thread will be present, but it might not be obvious that something's wrong because nothing will throw.darkmoon_uk
03/17/2021, 1:48 PM@ThreadLocal
be an actual language default; agree it could cause some serious confusion... but rather a suggested way to start for those wanting to avoid the potentially greater complications of object freezing.