Multiple AppDomains error "Cannot pass a GCHandle across AppDomains".
Hey team,
I recently stumbled upon this pretty nasty gotcha when trying to host our browser control inside a plugin system that loads a new AppDomains per plugin. When the native libcef.dll makes a call back into managed there is an exception thrown "Cannot pass a GCHandle across AppDomains".
I found that there have been several post written about this problem-
http://lambert.geek.nz/2007/05/29/unmanaged-appdomain-callback/
http://www.lenholgate.com/blog/2009/07/error-cannot-pass-a-gchandle-across-appdomains.html
Highlight from these posts-
"Consequently, when calling managed code from unmanaged code, the compiler has to pick one AppDomain to use, and it appears to pick the first one."
To test this issue
- Create a new WPF application
- Change App.xaml Build Action to Page (right-click Properties->Build Action->Page)
- Add file below with main - Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CefSharp;
namespace MultipleAppDomains
{
class Startup
{
[STAThread()]
static void Main()
{
AppDomain domain = AppDomain.CreateDomain("another domain");
CrossAppDomainDelegate action = () =>
{
var settings = new CefSettings
{
RemoteDebuggingPort = 8088,
BrowserSubprocessPath = "CefSharp.BrowserSubprocess.exe",
LogSeverity = LogSeverity.Verbose
};
if (!Cef.Initialize(settings))
{
// Do Something
}
App app = new App();
app.MainWindow = new MainWindow();
app.MainWindow.Show();
app.Run();
};
domain.DoCallBack(action);
}
}
}
I'd like to get the discussion going and come up with a good course of action.