Clearing all the references to a handle class object will remove it from memory, and the delete
method on your myClass
will be called automatically on upon the object being destroyed. It's just unfortunate confusing terminology. Your myClass
is an "MCOS class" or "MCOS object", not a "handle graphics object" like help clear
is talking about. They're different things, at least at the M-code level.
The handle graphics "objects" are not the same type of "object" that your myClass is, and the "handle graphics handle" returned by figure()
is not the same sort of thing as the class named handle
that you're inheriting from. That passage from help clear
talking about "handle graphics handles" doesn't apply to your object. See doc handle
and follow the link for the delete
method for relevant doco.
Don't feel bad; the Matlab doco doesn't make these distinctions very clear. (IIRC it doesn't even explicitly use the term "MCOS"; it just calls them "objects".) Basically, the material under the "Object-Oriented Programming" section in the doco is relevant to the kind of "object" and "handle" you're working on with myClass
. The doco under "Graphics" and "GUI Development" is talking about the other handle graphics kind of "object" and "handle". I think they use the term "handle" for the handle graphics stuff and "handle class" for the OOP stuff.
To verify that your delete
works, just make a trivial class.
classdef myClass < handle
methods
function delete(obj)
disp('delete was called');
end
end
end
And then create one and clear it.
>> x = myClass
x =
myClass handle with no properties.
Methods, Events, Superclasses
>> clear x
delete was called
>>
If your destructor is not being called, there may be other references to the object lingering. The destructor doesn't get called each time a variable holding a reference to the object is cleared, only when the last variable holding a reference (or indirect reference) is cleared.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…