Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: Class destructor not called during clear?

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!

like image 524
Shai Avatar asked Dec 27 '12 15:12

Shai


1 Answers

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
like image 106
Jonas Avatar answered Oct 24 '22 08:10

Jonas