Hi guys,
I have been using MATLAB Compiler R2017b to generate .NET Assembly files (.dll) from my *.m files, to be used in my C# console application.
Technically, once a .dll is loaded into an application at Runtime it cannot be unloaded unless the application is stopped, yet I want my App to run without stopping.
With some help on the internet I have found a way to load/unload my assembly files dynamically by creating a temporary sub-domain, load my MATLAB-compiled assembly inside it, and drop the domain afterwards. (See: How to unload a dll at Runtime) The problem is, my .m files are also trying to load some dlls. Most of you must have used the commands below like:
privateName = 'd:\my_private.dll';
NET.addAssembly(privateName)
to load whatever .dll you have to make it visible to MATLAB.
However this command doesn't allow you to specify which AppDomain you're adding your .dll to.
Say I have a MATLAB function to do such thing and I want to compile this function to get a LoadAssemblyAndWork.dll
function LoadAssemblyAndWork()
privateName = 'd:\my_private.dll';
asm = NET.addAssembly(privateName);
myClass = asm.Classes;
end
Back in my console application, when I load LoadAssemblyAndWork.dll in my sub-domain which tries to unpack my_private.dll , this secondary .dll was loaded into the main AppDomain instead of just the sub-domain where I'm in. The secondary .dll is still visible by the sub-domain and the job within my compiled function could still be carried out, however this secondary .dll still clings with the main AppDomain after the sub-domain was dropped when the job is done. It would eventually cause a memory problem if there are more and more .dlls loaded to my main AppDomain!
Generally speaking, I couldn't find a way to unload my .dll efficiently (or at least load it in a sub-domain which I expect), since the NET.addAssembly command from MATLAB doesn't seem to cover this scenario.
Some people on the internet called it a "leaking" problem. Has anyone ever come across such topic?