16

In Matlab I have a class

classdef myClass
    properties
       % some properties here...
    end
    methods ( Access = 'public' )
        function obj = myClass()
            % constructor...
        end
        function obj = delete( obj )
             % suppose to be destructor...
             fprintf(1, 'delete\n');
        end
    end % public methods
end

What is the default behavior of Matlab when I clear a variable of type myClass? For example:

>> m = myClass();
>> clear m

I would expect Matlab to call the destructor of m at this stage, but it seems like it doesn't!!

My questions:

  1. How can I force a call to the destructor when clearing a variable?

  2. Is this default behavior of Matlab's makes any sense? Isn't it more logical to call the destructor when clearing a variable?

  3. Is it possible that Matlab's classes do not have a detructor method (that is, there is no default method to be called when the class is destroyed)? Or am I missing something?

  4. Is it possible that only classes derived from handle have a destructor (the delete method)?

Thanks!

EDIT : following Jonas' answer, a brief summary:

Matlab has two types of classes: value classes (the default) and handle classes (derived from handle super class). Value classes tends to provide better performance, however, they do not have a destructor functionality.

handle classes do have a destructor function: delete that is called when the class is destroyed. See this question from more details on handle class destructors.

If one wishes to have a destructor-like functionality for value classes, Jona's answer proposes a method utilizing onCleanup functionality.

Thanks for the good answer and the insightful comments!

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    Possible dubplicate of: http://stackoverflow.com/questions/7236649/matlab-run-object-destructor-when-using-clear – Dennis Jaheruddin Dec 27 '12 at 15:52
  • 1
    @DennisJaheruddin - I suspect these two questions are related, but not duplicates, as the discussion there involved classes derived from the `handle` superclass. – Shai Dec 27 '12 at 15:59
  • 1
    I agree with Shai, it's a different (and interesting) issue. – Jonas Dec 27 '12 at 16:16
  • 1
    The bottom of this page seems to have some relevant info for your final questions. http://www.mathworks.nl/help/matlab/matlab_oop/handle-class-destructors.html – Dennis Jaheruddin Dec 27 '12 at 16:35
  • Another issue with MATLAB OOP: Simple assignment operator (e.g. A=B) cannot be overloaded. Or am I missing something? – Pavel Holoborodko Dec 01 '15 at 10:57
  • @PavelHoloborodko see: http://www.mathworks.com/matlabcentral/newsreader/view_thread/158254 – Shai Dec 01 '15 at 11:11
  • MATLAB doesn't call the class constructor in simple assignment like A = B. At least for value objects – Pavel Holoborodko Dec 01 '15 at 13:32
  • @PavelHoloborodko I guess you need to work with `handle` classes to get this functionality. – Shai Dec 01 '15 at 13:58
  • Same for handle classes. Assignment is not allowed to overload at all. – Pavel Holoborodko Dec 02 '15 at 02:41
  • @PavelHoloborodko it seems like instead of overloading the assignment operator, you need to implement a copy constructor and replace `a=b` with `a(b)`. – Shai Dec 02 '15 at 20:55
  • Well, I have no control over the user code. I just supply my new data type and existing code should work with it without changes thanks to overloaded operators (the C++ OOP style). Here is what I do: http://www.advanpix.com/ Restriction on overloading the assignment operator makes OOP in MATLAB very inefficient, if it can be called OOP at all. – Pavel Holoborodko Dec 03 '15 at 05:06

1 Answers1

14

Delete is only defined as class destructor for handle classes, not value classes (so the answer to Q4 is "yes", see the previous link to the documentation). Value classes work very much like standard Matlab arrays, in that they're passed by value rather than by reference, and in that many of the internals, such as destructors, are hidden from the user. In exchange, they're usually faster (see for example this SO question).

Consequently, I suggest to use the onCleanup functionality if you want to have a delete method being called (note that delete(m) will not actually delete anything, so you may want to make that a private method).

classdef myTestClass
    properties
       % some properties here...
    end
    properties (Hidden)
        cleanup
    end
    methods ( Access = 'public' )
        function obj = myTestClass()
            % constructor...
            obj.cleanup = onCleanup(@()delete(obj));
        end
    end 
    methods ( Access = 'private' )
        %# I suggest hiding the delete method, since it does not
        %# actually delete anything
        function obj = delete( obj )
             fprintf(1, 'delete\n');
        end
    end % public methods
end
Community
  • 1
  • 1
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • thanks for the answer. However, isn't there a more straightforward way of defining a destructor that is called when a class instance is deleted / cleared? I'm more of a C++ OOP person, so I can't really see why Matlab is making this destructor thing so non-trivial... – Shai Dec 27 '12 at 16:27
  • 2
    @Shai: If you come from C++ OOP, I suggest using handle classes instead of value classes - the destructor works as expected, you pass by reference, both which are features you'd expect coming from C++. – Jonas Dec 27 '12 at 16:42
  • 1
    @Shai: I've expanded my answer to explicitly touch questions 3 and 4. – Jonas Dec 27 '12 at 16:47