What happens to cleared custom `handle` objects?
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!