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

2 visualizaciones (últimos 30 días)
Florian Berzsenyi
Florian Berzsenyi el 17 de Dic. de 2021
Respondida: Ishaan Mehta el 12 de Feb. de 2024
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) % ? ? ? ?

Respuesta aceptada

Ishaan Mehta
Ishaan Mehta el 12 de Feb. de 2024
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!

Más respuestas (0)

Categorías

Más información sobre Use COM Objects in MATLAB en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by