What happens to cleared custom `handle` objects?

조회 수: 1 (최근 30일)
Rohan Kadambi
Rohan Kadambi 2024년 5월 20일
편집: Matt J 2024년 5월 20일
I know that MATLAB exposes the handle class which alllows one to define passed-by-reference objects like:
classdef ConcreteHandle < handle
properties
UserData = []
end
methods
function self = ConcreteHandle(data)
arguments
data = []
end
self.UserData = data;
end
end
end
How does the behaviour of this class differ from the existing graphical handle objects? For example:
fh = figure;
clear fh
Doesn't delete the figure (but close(fh) or delete(fh) will). Even after the reference to fh is cleared you can still recover it with built-in utilities like findall():
fh = findall(0, 'Type', 'Figure');
What happens if I construct an instance of my custom class:
c = ConcreteHandle();
clear c
Is c (and the data in c.UserData) handed over to the gargabe collector? Does there exist an equivilant call to findall() or a similar utility that lets me find c?

채택된 답변

Matt J
Matt J 2024년 5월 20일
편집: Matt J 2024년 5월 20일
If you clear c, then that copy of the handle is gone, while other shared instances of it that you may have created remain. If you clear() every last shared instance that you've created, then the handle object is irretrievably gone.
The same is true of graphics handles. The reason findall can recover a handle to an open figure is because there is no way to clear() every shared instance of a handle to an open figure. This is because groot stores a shared instance of the handle in its Children property, which remains there until the figure is closed or deleted.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by