Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
- Checkout email@example.com:xamarin/Profiler (I have 55fc81632e37bb0e38de24f086933affc5695dba)
- git revert -n 96d2ff6e8b0d1a1c83b40f1132db101da6f4dce6 (This is a work around hack)
- Apply this: https://gist.github.com/rolfbjarne/f0d690b0e68c96039ddd (needed because the XM class libs haven't been properly audited for NRC yet)
- Open solution, enable NRC, build & run
- It'll crash right away.
- NSCell implements copyWithZone: using NSCopyObject, which is evil .
- NSCopyObject will do a bitwise copy of all the ivars (without any other processing), which includes copying our GCHandle stored in the __monoObjectGCHandle field.
- Suddenly there are two different native objects pointing to the same managed object through the __monoObjectGCHandle field.
- One of those native objects is freed. The GCHandle will be disposed.
- Now there is a native object with an invalid GCHandle.
- Some other managed object creates a GCHandle with the same numerical valid as the invalid one.
- The copied native object now points to a different managed object (potentially with a completely different type).
- OSX calls a method on the copied native object, we fetch the "corresponding" managed object, and bad things happen when we call an instance in type FOO where the instance is type BAR.
Hacky workaround: https://gist.github.com/rolfbjarne/d9cbf624ba10c2c77159
- This works by clearing out the __monoObjectGCHandle field when the object is copied, so that the GCHandle doesn't end up duplicated.
Proper fix: detect when a user class implements INSCopying and implement copyWithZone: automagically. This also needs to work when the user overrides copyWithZone:.
 https://www.mikeash.com/pyblog/friday-qa-2010-08-27-defensive-programming-in-cocoa.html - section about "Copying"