[Mono-dev] reloading mono domain or assemblies

Lucas Meijer lucas at lucasmeijer.com
Fri Aug 13 12:19:03 EDT 2010


  On 8/13/10 4:04 PM, Robert Jordan wrote:
> Well, it doesn't really matter who's creating the domains
> and loading the assemblies. It's by far easier to implement
> this in C# and then mono_runtime_invoke it from C++.
>
> Of course, you can invoke everything from C++ but this will
> likely triplicate the amount of code to be written.
>
> Furthermore, the only safe way to create a domain in C++ is
> mono_runtime_invoke-ing System.AppDomain.CreateDomain(), IIRC.
you can use mono_domain_create() and mono_domain_unload() just fine from 
c.  In fact, I suspect it's actually
easier to do from native code these days.  basically what you should do is:

create domain
set it active
load your assemblies
run your code

when you want to reload code,

unload the domain
load your assemblies again
run your code.

It gets tricky though, as you need to somehow maintain the "state" of 
your program. We use custom serialization
to remember the state of all objects,   tear down everything,  load new 
assemblies,  recreate all objects, and reconfigure them the way they were.
not an easy task by any means.

also make sure you load your assemblies from memory instead of from 
disk.  at least on windows, if you read them from disk, you will not be 
allowed to
overwrite your assemblies with new ones.

domain unloading/reloading is a bit of a notorious area, with plenty of 
memory leaks and bugs.  mostly because it is a way to use mono that not that
many people seem to be doing.  I would say from all the mono related 
work I do at Unity, about 80% is related to problems relating to domain 
unloading.

Bye, Lucas


More information about the Mono-devel-list mailing list