Search code examples
c++cmonoappdomainembedding

Unloading Mono domains in multithreaded context


I have a multithreaded application that activate multiple Mono domains from native code. Each domain has it's own thread. I use the following code to activate a domain:

///Create a new domain.
m_domain = mono_domain_create_appdomain((char*) name.c_str(), NULL);

///Activate the domain.
mono_domain_set(m_domain, 0);

///Register the current thread
mono_thread_attach(m_domain);

///Invoke some code ...
mono_runtime_invoke (m_method, m_objectInstance, NULL, &exception);

But when I unload a domain the application crash :

mono_domain_unload(m_domain);

When I execute the code without threading, the application domains are unloaded correctly.


Solution

  • I was calling the function mono_assembly_close, before mono_domain_unload :

    mono_assembly_close(m_assembly);
    

    So I removed this call, and created a critical section for the unloading code.