Open filipnavara opened 1 month ago
This is a tricky problem, and we've run into different variations on it over the years.
Make the
Dispose
on immutable poolable classes likeNSString
a no-op? Make an analyzer that warns when someone tries to dispose aNSString
instance (would flag the obvious error but not if someone disposes it asNSObject
variable)?
The problem with this is that I can certainly envision someone wanting to dispose at least some NSString
instances (they can be arbitrarily big, so you might want to dispose big ones).
One idea I just had would be to just skip disposing tagged pointers, but the main problem with this approach is that there's no reliable way to detect tagged pointers (Apple can change their implementation at any time). Just doing a string comparison on the Objective-C class (NSTaggedPointerString
), but it's a bit scary to have correctness depend on this (if Apple changed this class's name, apps could randomly start throwing ObjectDisposedException...)
Presumably, if this is affecting all tagged objects, we can differentiate by the lowest (x64) / highest (arm64) bit == 1 and disallow Dispose
on those (disallow as in make it a no-op). I don't like the fact that it's dependency on internal implementation detail but it's probably just as reliable as checking for NSTaggedPointerString
and it solves the same issue for NSNumber
(and NSColor
?).
I'm not sure if there are any other cases of reused objects aside from the tagged ones.
Presumably, if this is affecting all tagged objects, we can differentiate by the lowest (x64) / highest (arm64) bit == 1 and disallow
Dispose
on those (disallow as in make it a no-op). I don't like the fact that it's dependency on internal implementation detail
That internal implementation detail changed in iOS 14: "Let's explore one more change coming this year. A change to the tagged pointer format on arm64." - https://developer.apple.com/videos/play/wwdc2020/10163/
Which means we'll have to detect (and not apply the tagged pointer logic) when running on iOS 13 or earlier (our min iOS is right now iOS 12.2 in .NET 9). Alternatively not enable it when an app's min OS version is <14.
I think the part about the tagging logic that changed doesn't affect the "is tagged" bit, only the other ones which we do not rely on.
Consider the following code:
It will reliably crash with
ObjectDisposedException
which may be quite unexpected. In fact, this is a very reduced example of a problem that was happening in a multi-threaded application where it was even less obvious.Why does it crash?
NSString
object representing the stringaps
.aps
key, and that in turns maps to the same managedNSString
object.Dispose
on theNSString
causes the managed representation to be invalid and next enumeration of the same key will crash withObjectDisposedException
.What can we do about it?
Changing the behavior to remove disposed objects from the handle->managed object mapping seems dangerous.(would not help anyway in the original multi-threaded scenario)Make the
Dispose
on immutable poolable classes likeNSString
a no-op? Make an analyzer that warns when someone tries to dispose aNSString
instance (would flag the obvious error but not if someone disposes it asNSObject
variable)?Thoughts?