login about faq

C# vs 2013. When I try to run the following code in a unit test, I get the following exception. Any ideas why?

http://www.example-code.com/csharp/async_task.asp

System.ArgumentException was unhandled Message: An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Cannot pass a GCHandle across AppDomains.

asked Sep 23 '15 at 00:40

ftper's gravatar image

ftper
162


Chilkat stores a reference to the managed object in gcroot in cli (native code) for event callbacks. In a normal synchronous call, this is on the stack and the reference is destructed when the method returns. In the async case, the gcroot survives while the task is running. I suspect this is related to the problem.

In order to have event callbacks, a reference to the object must be stored by the native code, and it must be stored via gcroot. The .NET runtime must know that something is still referencing the managed object. Apparently, gcroot's cannot be used across AppDomains, or maybe there is a workaround -- I'm not sure at this point. (Google search "unmanaged appdomain callback")

I wonder if the unit testing features of Visual Studio are introducing separate AppDomains? This has been tested in Visual Studio by creating a simple forms app..

link

answered Sep 23 '15 at 12:29

chilkat's gravatar image

chilkat ♦♦
11.8k316358421

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×80
×31
×22

Asked: Sep 23 '15 at 00:40

Seen: 1,809 times

Last updated: Sep 23 '15 at 12:29

powered by OSQA