MATLAB Object-Oriented Programming

(Joyce) #1

  • No longer referenced anywhere

  • Explicitly deleted by calling delete on the handle


Inside a Function


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 in 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 and 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.


Sequence During Handle Object Destruction


MATLAB invokes the delete methods in the following sequence when destroying an
object:


(^1) The delete method for the class of the object
(^2) The 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
specifies supclass1 before supclass2. MATLAB calls the delete method of
supclass1 before the delete method of supclass2.
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 a call the delete methods for those objects when
there are no other references to those objects.
Superclass delete methods cannot call methods or access properties belonging to a
subclass.
Handle Class Destructor

Free download pdf