Do I have to call release() on matlab COM-interfaces?

4 次查看(过去 30 天)
Take a look at the following MATLAB example (copied from there), demonstrating collections:
hCollection = hControl.Plots;
for i = 1:hCollection.Count
hPlot = invoke(hCollection,'Item', i);
Redraw(hPlot)
release(hPlot);
end;
release(hCollection);
My simple question now is: Do I have to call relase on every COM-interface I generate, e.g. by calling invoke()? Currently I do not, as I thought that the MATLAB garbage collector takes care of reference counting and memory release.
Another example below might refine my question. When will the interface "Sheets" be allocated, and not just referenced?
newWBook = xlApp.Sheets.Open(*name*)
% vs.
wBooks = xlApp.Sheets % or invoke(xlApp, 'Sheets')
newWBook = wBooks.Sheets
release(wBooks) % ? ? ? ?

采纳的回答

Ishaan Mehta
Ishaan Mehta 2024-2-12
Hi Florian,
From the code snippets shared by you, I understand that you wish to check if "release" function in MATLAB mandatorily to release a COM interface.
I checked MathWorks documentation regarding the same, and it states that in MATLAB versions prior to 6.5, not manually releasing interface handles or not properly deleting the server could lead to memory leaks. This issue would occur even when the variable associated with the interface or COM object was allocated to something else. From MATLAB version 6.5 onwards, the system automatically disposes of the server and all related interfaces when the corresponding variable is either reassigned or goes out of scope, effectively preventing such memory leaks.
Nonetheless, if there is a need to promptly relinquish a COM object or interface and liberate all tied resources, invoking the "release" function on the COM object is a useful option. This action can be particularly beneficial in scenarios where numerous COM objects are in use or when the COM server is constrained by resource limitations.
Hope this helps!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by