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 adelete
method. The generated code calls thedelete
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 therelease
method, which in turn calls the user-definedreleaseImpl
. For more information on when to define adelete
method in a MATLAB code, seeHandle Class Destructor.
When you write the MATLAB code, adhere to these guidelines and restrictions:
Code generation does not support recursive calls of thedelete
method. Do not create an object of a certain class inside thedelete
method for the same class. This usage might cause a recursive call ofdelete
and result in an error message.
The generated code always calls thedelete
method, when an object goes out of scope. Code generation does not support explicit calls of thedelete
method.
Initialize all properties ofMyClass
that thedelete
method ofMyClass
uses either in the constructor or as the default property value. Ifdelete
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 propertyprop1
ofMyClass1
is itself an object (an instance of another classMyClass2
). Initialize all properties ofMyClass2
that thedelete
method ofMyClass1
uses. Perform this initialization either in the constructor ofMyClass2
or as the default property value. Ifdelete
tries to access a property ofMyClass2
that has not been initialized in one of these two ways, the code generator produces an error message. For example, define the two classesMyClass1
andMyClass2
:
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.prop2
that thedelete
method displays.
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 causesdelete
to 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 = MyClass
inMyFunction
. MATLAB calls thedelete
method for the first object after the second instance ofobj = MyClass
inMyFunction
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, bothdelete
method calls happen at the end of the function when the two objects go out of scope. RunningMyFunction_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 theterminate
function explicitly to destroy thepersistent
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 thedelete
method for that object. For a System object™, if there is a run-time error insetupImpl
, the generated code does not callreleaseImpl
for that object.
MATLAB does call thedelete
method to destroy a partially constructed object.