Bug 41202 - "System.ArgumentException: GCHandle value belongs to a different domain" is thrown when I hold strong reference with GCHandle and delete it in finalizer
Summary: "System.ArgumentException: GCHandle value belongs to a different domain" is t...
Alias: None
Product: Runtime
Classification: Mono
Component: GC ()
Version: 4.2.0 (C6)
Hardware: PC Windows
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Vlad Brezae
Depends on:
Reported: 2016-05-20 08:03 UTC by xvroad@gmail.com
Modified: 2017-11-21 21:02 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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 original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description xvroad@gmail.com 2016-05-20 08:03:13 UTC
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.


Unhandled Exception:
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
lename unknown>:0
  at cs.ndll.CSAbstract.Finalize () <0x8ccb1e8 + 0x0001b> in <filename unknown>:
Comment 1 xvroad@gmail.com 2016-05-20 09:41:42 UTC
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.
Comment 2 xvroad@gmail.com 2016-05-20 13:28:43 UTC
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.
Comment 3 Rodrigo Kumpera 2016-07-21 05:39:54 UTC

Can you take a look at this issue?
Comment 4 Ludovic Henry 2017-11-10 21:16:35 UTC
Can you still reproduce with latest Mono?
Comment 5 Ludovic Henry 2017-11-13 22:46:28 UTC
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.
Comment 6 Rodrigo Kumpera 2017-11-15 18:48:17 UTC
Does your code uses multiple appdomains?
Could you, by change, be calling code from another domain when passing that handle?