Main Content

Code Generation for Handle Class Destructors

You can generate code for MATLAB® code that uses delete methods (destructors) for handle classes. To perform clean-up operations, such as closing a previously opened file before an object is destroyed, use a delete method. The generated code calls the delete method at the end of an object's lifetime, even if execution is interrupted by a run-time error. When System objects are destroyed, delete calls the release method, which in turn calls the user-defined releaseImpl. For more information on when to define a delete method in a MATLAB code, see Handle Class Destructor.

Guidelines and Restrictions

When you write the MATLAB code, adhere to these guidelines and restrictions:

  • Code generation does not support recursive calls of the delete method. Do not create an object of a certain class inside the delete method for the same class. This usage might cause a recursive call of delete and result in an error message.

  • The generated code always calls the delete method, when an object goes out of scope. Code generation does not support explicit calls of the delete method.

  • Initialize all properties of MyClass that the delete method of MyClass uses either in the constructor or as the default property value. If delete tries to access a property that has not been initialized in one of these two ways, the code generator produces an error message.

  • Suppose a property prop1 of MyClass1 is itself an object (an instance of another class MyClass2). Initialize all properties of MyClass2 that the delete method of MyClass1 uses. Perform this initialization either in the constructor of MyClass2 or as the default property value. If delete tries to access a property of MyClass2 that has not been initialized in one of these two ways, the code generator produces an error message. For example, define the two classes MyClass1 and MyClass2:

    classdef MyClass1 < handle
        properties
            prop1
        end
        methods
            function h = MyClass1(index)
                h.prop1 = index;
            end
            function delete(h)
                fprintf('h.prop1.prop2 is: %1.0f\n',h.prop1.prop2);
            end
        end
    end
    classdef MyClass2 < handle
        properties
            prop2
        end
    end

    Suppose you try to generate code for this function:

    function MyFunction
    obj2 = MyClass2;
    obj1 = MyClass1(obj2); % Assign obj1.prop1 to the input (obj2)
    end

    The code generator produces an error message because you have not initialized the property obj2.prop2 that the delete method displays.

Behavioral Differences of Objects in Generated Code and in MATLAB

The behavior of objects in the generated code can be different from their behavior in MATLAB in these situations:

  • The order of destruction of several independent objects might be different in MATLAB than in the generated code.

  • The lifetime of objects in the generated code can be different from their lifetime in MATLAB. MATLAB calls the delete method when an object can no longer be reached from any live variable. The generated code calls the delete method when an object goes out of scope. In some situations, this difference causes delete to be called later on in the generated code than in MATLAB. For example, define the class:

    classdef MyClass < handle
        methods
            function delete(h)
                global g
                % Destructor displays current value of global variable g
                fprintf('The global variable is: %1.0f\n',g);
            end
        end
    end

    Run the function:

    function MyFunction
    global g
    g = 1;
    obj = MyClass;
    obj = MyClass;
    % MATLAB destroys the first object here
    g = 2;
    % MATLAB destroys the second object here
    % Generated code destroys both objects here
    end

    The first object can no longer be reached from any live variable after the second instance of obj = MyClass in MyFunction. MATLAB calls the delete method for the first object after the second instance of obj = MyClass in MyFunction and for the second object at the end of the function. The output is:

    The global variable is: 1
    The global variable is: 2

    In the generated code, both delete method calls happen at the end of the function when the two objects go out of scope. Running MyFunction_mex results in a different output:

    The global variable is: 2
    The global variable is: 2
  • In MATLAB, persistent objects are automatically destroyed when they cannot be reached from any live variable. In the generated code, you have to call the terminate function explicitly to destroy the persistent objects.

  • The generated code does not destroy partially constructed objects. If a handle object is not fully constructed at run time, the generated code produces an error message but does not call the delete method for that object. For a System object™, if there is a run-time error in setupImpl, the generated code does not call releaseImpl for that object.

    MATLAB does call the delete method to destroy a partially constructed object.

Related Topics