Main Content

Code Generation for Handle Class Destructors

You can generate code for MATLAB®code that usesdelete方法(destructors) for handle classes. To perform clean-up operations, such as closing a previously opened file before an object is destroyed, use adeletemethod. The generated code calls thedeletemethod at the end of an object's lifetime, even if execution is interrupted by a run-time error. When System objects are destroyed,deletecalls thereleasemethod, which in turn calls the user-definedreleaseImpl. For more information on when to define adeletemethod in a MATLAB code, seeHandle 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 thedeletemethod. Do not create an object of a certain class inside thedeletemethod for the same class. This usage might cause a recursive call ofdeleteand result in an error message.

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

  • Initialize all properties ofMyClassthat thedeletemethod ofMyClassuses either in the constructor or as the default property value. Ifdeletetries to access a property that has not been initialized in one of these two ways, the code generator produces an error message.

  • Suppose a propertyprop1ofMyClass1is itself an object (an instance of another classMyClass2). Initialize all properties ofMyClass2that thedeletemethod ofMyClass1uses. Perform this initialization either in the constructor ofMyClass2or as the default property value. Ifdeletetries to access a property ofMyClass2that has not been initialized in one of these two ways, the code generator produces an error message. For example, define the two classesMyClass1andMyClass2:

    classdefMyClass1 < handlepropertiesprop1end方法functionh = MyClass1(index) h.prop1 = index;endfunctiondelete(h) fprintf('h.prop1.prop2 is: %1.0f\n',h.prop1.prop2);endendend
    classdefMyClass2 < handlepropertiesprop2endend

    Suppose you try to generate code for this function:

    functionMyFunction 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 propertyobj2.prop2that thedeletemethod displays.

Behavioral Differences of Objects in Generated Code and inMATLAB

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 thedelete方法时an object can no longer be reached from any live variable. The generated code calls thedelete方法时an object goes out of scope. In some situations, this difference causesdeleteto be called later on in the generated code than in MATLAB. For example, define the class:

    classdefMyClass < handle方法functiondelete(h)globalg% Destructor displays current value of global variable gfprintf('The global variable is: %1.0f\n',g);endendend

    Run the function:

    functionMyFunctionglobalg g = 1;obj = MyClass;obj = MyClass;% MATLAB destroys the first object hereg = 2;% MATLAB destroys the second object here% Generated code destroys both objects hereend

    The first object can no longer be reached from any live variable after the second instance ofobj = MyClassinMyFunction. MATLAB calls thedeletemethod for the first object after the second instance ofobj = MyClassinMyFunctionand 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, bothdeletemethod calls happen at the end of the function when the two objects go out of scope. RunningMyFunction_mexresults in a different output:

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

  • 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 thedeletemethod for that object. For a System object™, if there is a run-time error insetupImpl, the generated code does not callreleaseImplfor that object.

    MATLAB does call thedeletemethod to destroy a partially constructed object.

Related Topics