Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

oop - MATLAB - run object destructor when using 'clear'?

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 clearing 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...