When destroying an object, MATLAB calls the class destructor
method, if the class defines one. Create a destructor method by implementing
a method named
delete. However, MATLAB recognizes
a class method named
delete as the class destructor
only if you define
delete as an ordinary method
with the appropriate syntax.
To be a valid class destructor, the
Must have one scalar input argument, which is an object of the class.
Must not define output arguments
In addition, destructors should not:
Create new handles to the object being destroyed
MATLAB does not call a noncompliant
when destroying objects of the class.
delete as an ordinary method:
methods function delete(obj) % obj is always scalar ... end end
MATLAB calls the destructor element-wise on an array of
objects. Because MATLAB calls the
separately for each element in an object array, a
is passed only one scalar argument.
delete on a deleted handle should
not error and can take no action. This enables
work on object arrays containing a mix of valid and invalid objects.
Use a class destructor to perform any necessary cleanup operations before MATLAB destroys the object.
For example, suppose an object method opens a file for writing
and you want to close the file in your
delete method calls
a file identifier that is stored in the object
function delete(obj) fclose(obj.FileID); end
For an example, see Class to Manage Writable Files.
If you create a hierarchy of classes, each class can define
its own class destructor. When destroying an object, MATLAB calls
delete method of each class in the hierarchy.
delete method in a
does not override the
delete methods augment the superclass
You cannot define a valid destructor that is
Sealed. MATLAB returns
an error when you attempt to instantiate a class that defines a
Normally, declaring a method as
subclasses from overriding that method. However, because destructors
must be named
delete, an inherited method named
Sealed does not prevent subclasses from defining
For example, if a superclass defines a method named
is not a valid destructor and is
Sealed, then subclasses:
Can define valid destructors (which are always named
Cannot define methods named
are not valid destructors.
Heterogeneous class hierarchies require that all methods to which heterogeneous arrays are passed must be sealed. However, the rule does not apply to class destructor methods. Because destructor methods cannot be sealed, you can define a valid destructor in a heterogeneous hierarchy that is not sealed, but does function as a destructor.
For information on heterogeneous hierarchies, see Heterogeneous Arrays
MATLAB invokes the
delete method when
the lifecycle of an object ends. The lifecycle of an object ends when
the object is:
No longer referenced anywhere
Explicitly deleted by calling
The lifecycle of an object referenced by a local variable or input argument exists from the time the variable is assigned until the time it is reassigned, cleared, or no longer referenced within that function or any handle array.
A variable goes out of scope when you explicitly clear it or
when its function ends. When a variable goes out of scope, if its
value belongs to a handle class that defines a
delete method, MATLAB calls
that method. MATLAB defines no ordering among variables in a
function. Do not assume that MATLAB destroys one value before
another value when the same function contains multiple values.
MATLAB invokes the
delete methods in
the following sequence when destroying an object:
delete method for the class
of the object
delete method of each superclass
class, starting with the immediate superclasses and working up the
hierarchy to the most general superclasses
MATLAB invokes the
delete methods of
superclasses at the same level in the hierarchy in the order specified
in the class definition. For example, the following class definition
supclass2. MATLAB calls
delete method of
delete method of
classdef myClass < supclass1 & supclass2
After calling each
delete method, MATLAB destroys
the property values belonging exclusively to the class whose method
was called. The destruction of property values that contain other
handle objects can cause MATLAB to call the
for those objects, if there are no other references to those objects.
delete methods cannot call methods
or access properties belonging to a subclass.
Consider a set of objects that reference other objects of the set such that the references form a cyclic graph. In this case, MATLAB:
Destroys the objects if they are referenced only within the cycle
Does not destroy the objects as long as there is an external reference to any of the objects from a MATLAB variable outside the cycle
MATLAB destroys the objects in the reverse of the order of construction.
You can destroy handle objects by explicitly calling
A class can prevent explicit destruction of an object by setting
private. However, a method of the class can
If the class
protected, only methods of the class and of
subclasses can explicitly delete objects of that class.
However, when an object's lifecycle ends, MATLAB calls
delete method when destroying
the object regardless of method's
Class destructor behavior differs from the normal behavior of
an overridden method. MATLAB executes each
of each superclass upon destruction, even if that
When you explicitly call an object's
delete method, MATLAB checks
in the class defining the object, but not in the superclasses of the
object. A superclass with a
cannot prevent the destruction of subclass objects.
Declaring a private delete method makes most sense for sealed
classes. In the case where classes are not sealed, subclasses can
define their own delete methods with public access. MATLAB calls
a private superclass
delete method as a result
of an explicit call to a public subclass
A class can implement a method named
is not a valid class destructor, and therefore is not called implicitly
by MATLAB when destroying an object. In this case,
like a normal method.
For example, if the superclass implements a
delete that is not a valid destructor, then MATLAB does
not allow subclasses to override this method.
delete method defined by a value class
cannot be a class destructor.