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 for Bug 41202 on
GitHub or Developer Community if you have new
information to add and do not yet see a matching new report.
If the latest results still closely match this report, you can use the
In special cases on GitHub you might also want the comments:
GitHub Markdown with public comments
This code runs fine on Microsoft's runtime, but not on Mono for Windows v4.2.3. My application exits with exception when I close it. Is this because I'm allocating GCHandle inside finalizer?
I sometimes don't get this error for some reason.
System.ArgumentException: GCHandle value belongs to a different domain
at System.Runtime.InteropServices.GCHandle.op_Explicit (IntPtr value) <0x36c84
08 + 0x00093> in <filename unknown>:0
at System.Runtime.InteropServices.GCHandle.FromIntPtr (IntPtr value) <0x36c83d
0 + 0x00017> in <filename unknown>:0
at cs.ndll.HandleUtils.GetObjectFromIntPtr (IntPtr ptr) <0x36c8370 + 0x00033>
in <filename unknown>:0
at cs.ndll.CFFICSLoader.cs_destroy_root (IntPtr inValue) <0x3712280 + 0x00013>
in <filename unknown>:0
at (wrapper native-to-managed) cs.ndll.CFFICSLoader:cs_destroy_root (intptr)
at (wrapper managed-to-native) System.Object:wrapper_native_03C92590 (intptr)
at cs.ndll.CSAbstract.Dispose (Boolean disposing) <0x8ccb230 + 0x00068> in <fi
at cs.ndll.CSAbstract.Finalize () <0x8ccb1e8 + 0x0001b> in <filename unknown>:
It turned out to be another issue. This only occurs when I hold strong reference from C++ with GCHandle, and release it in C# finalizer by calling C++ cleanup function.
I avoided this problem by moving GCHandle to outside of CSPersistent class. I don't know why Microsoft's runtime didn't throw exceptions or crash, maybe it's handling this issue in a different way. Does this suggest that another bug exists in Mono, or is it completely my fault?
I've also tried older code on v4.4.0. Mono crashed without showing error messages.
So I learned that I shouldn't hold reference to this object and free it in Dispose method.
This will cause circular reference issues, at least on Mono.
Can you take a look at this issue?
Can you still reproduce with latest Mono?
Vlad, please try to come up with a test case that could reproduce that issue. Feel free to switch back to NEEDINFO if you cannot find how to reproduce.
Does your code uses multiple appdomains?
Could you, by change, be calling code from another domain when passing that handle?