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
deletemethod. 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 the
deletemethod, when an object goes out of scope. Code generation does not support explicit calls of thedeletemethod.Initialize all properties of
MyClassthat 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 property
prop1ofMyClass1is 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: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.prop2that thedeletemethod 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
deletemethod when an object can no longer be reached from any live variable. The generated code calls thedeletemethod when 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: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 = 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, both
deletemethod 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 the
deletemethod 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 the
deletemethod to destroy a partially constructed object.