What happens to cleared custom `handle` objects?
    8 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Rohan Kadambi
 am 20 Mai 2024
  
    
    
    
    
    Bearbeitet: Matt J
      
      
 am 20 Mai 2024
            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 Kommentare
Akzeptierte Antwort
  Matt J
      
      
 am 20 Mai 2024
        
      Bearbeitet: Matt J
      
      
 am 20 Mai 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. 
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
				Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

