Suppose I have a class myClass < handle
. From the Mathworks Help page on clear
,
Clearing handle graphics handles does not remove the objects themselves, nor does deleting the objects remove variables storing their handles.
hf = figure; % Creates figure object, stores handle in variable hf
delete(hf) % Removes figure object, but not the variable hf
clear hf % Removes hf from the workspace; figure could still exist
So clear
ing a handle object does not remove it from memory unless I explicitly delete
it first..
I specified a destructor for myClass
to do proper cleanup and remove some references to it; this destructor is not called upon clear
. Is it possible to call that destructor when my object is cleared?
EDIT: I should mention that while delete
is automatically called with a clear
if there are no references to the myClass
object, I have another class, say myOtherClass
with properties that refer to myClass
, say myOtherClass.a
. There are additionally other properties in myOtherClass
that aren't properties of myClass
, but they should be empty if myOtherClass.a
is also empty. Let me know开发者_高级运维 if that doesn't make sense, that might have been a bit too wordy.
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.
精彩评论