Clearing handle subclasses with timer objects
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Oliver Woodford
am 30 Mai 2012
Kommentiert: Wilco Pancras
am 8 Jul. 2022
Save this class:
classdef test_class < handle
methods
function this = test_class()
fprintf('Hello world\n');
%timer('TimerFcn', @(a,b) fprintf('I''m alive\n'), 'Period', 1, 'ExecutionMode', 'fixedSpacing');
end
function delete(this)
fprintf('Goodbye world\n');
end
end
end
Now run this:
h = test_class(); clear;
Now uncomment the commented tine in test class and do the same again. You'll see that the delete() method isn't being called, though it should. Why?
Now call:
delete(timerfindall);
and the delete() method will be called. How can I get the delete method to be called when the handle to the object, as opposed to the timer (or the last of the two), is cleared?
I'm using R2012a on Windows 7 64-bit.
Since the documentation says: "If you call clear on all handle variables that refer to the same handle object, then you have lost access to the object and MATLAB destroys the object. That is, when there are no references to an object, the object ceases to exist." I believe what I'm seeing is a bug. The timer certainly shouldn't be creating a copy of the handle to the object.
0 Kommentare
Akzeptierte Antwort
per isakson
am 30 Mai 2012
My mental model says that "@(a,b)" contains a snapshot of the scope in which it was created. That snapshot includes "this". Thus, there is a reference to the object in the timer object.
0 Kommentare
Weitere Antworten (1)
Oliver Woodford
am 1 Jun. 2012
1 Kommentar
Wilco Pancras
am 8 Jul. 2022
Note that this is also true for value classes that own an onCleanup object.
Siehe auch
Kategorien
Mehr zu Class File Organization 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!