MATLAB - run object destructor when using 'clear'?
Asked Answered
W

1

11

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.

Widow answered 29/8, 2011 at 22:17 Comment(0)
J
13

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.

Janik answered 29/8, 2011 at 23:6 Comment(3)
There are certainly other references to the lingering object; part of the reason I would like this functionality is because I get rid of those references in my destructor. I updated the OP with the relevant information; it might not make sense without the complete context, but the complete context isn't relevant to the question in particular..Widow
I've had similar problems, especially when creating custom GUI elements. I don't think it's possible to force the destructor to be called when you clear the variable. As you note in your question, you may want to simply call the delete method on your object before clearing it.Inspan
Just FYI, I think it's good functionality that the destructor isn't called with clear is used in the terminal when using a gui with object oriented code (which I've mostly found to be the case). This means that if the user uses clear by accident, your gui won't shutdown, even if the handle to the gui is cleared from the workspace. However, if the user calls delete on the handle the destructor will still run.Schnitzel

© 2022 - 2024 — McMap. All rights reserved.