Clear Filters
Clear Filters

What happens to cleared custom `handle` objects?

2 views (last 30 days)
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?

Accepted Answer

Matt J
Matt J on 20 May 2024
Edited: Matt J on 20 May 2024
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.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!