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 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 the
delete
method, when an object goes out of scope. Code generation does not support explicit calls of thedelete
method.Initialize all properties of
MyClass
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 property
prop1
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
: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 thedelete
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 thedelete
method when 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: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
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, both
delete
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 the
delete
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 the
delete
method to destroy a partially constructed object.