Load .NET assembly in specific AppDomain?

2 ビュー (過去 30 日間)
Roger Tsang
Roger Tsang 2019 年 3 月 19 日
コメント済み: André Horn 2019 年 7 月 2 日
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;
% do anything with myClass
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?
  1 件のコメント
André Horn
André Horn 2019 年 7 月 2 日
Hi there,
I am very interested in the same question. We tried to start a matlab dll in a sepearate App Domain have the possibility to clean it up in strange cases.
Matlab seems to run (at least parts of it) in the default App Domain which makes any attempt in this direction fruitless.
Is thery any way to overcome this problem?

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB Compiler SDK についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by