MATLAB Object-Oriented Programming

(Joyce) #1

MATLAB calls a private superclass delete method as a result of an explicit call to a
public subclass delete method.


Nondestructor Delete Methods


A class can implement a method named delete that is not a valid class destructor.
MATLAB does not call this method implicitly when destroying an object. In this case,
delete behaves like an ordinary method.


For example, if the superclass implements a Sealed method named delete that is not a
valid destructor, then MATLAB does not allow subclasses to override this method.


A delete method defined by a value class cannot be a class destructor.


Java Objects Referencing MATLAB Objects


Java does not support the object destructors that MATLAB objects use. Therefore, it is
important to manage the lifecycle of all objects used in applications that include both Java
and MATLAB objects.


References Can Prevent Destructor Execution


Java objects that hold references to MATLAB objects can prevent deletion of the MATLAB
objects. In these cases, MATLAB does not call the handle object delete method even
when there is no handle variable referring to that object. To ensure your delete method
executes, call delete on the object explicitly before the handle variable goes out of
scope.


Problems can occur when you define callbacks for Java objects that reference MATLAB
objects.


For example, the CallbackWithJava class creates a Java
com.mathworks.jmi.Callback object and assigns a class method as the callback
function. The result is a Java object that has a reference to a handle object via the
function-handle callback.


classdef CallbackWithJava < handle
methods
function obj = CallbackWithJava
jo = com.mathworks.jmi.Callback;
set(jo,'DelayedCallback',@obj.cbFunc); % Assign method as callback
jo.postCallback
end


Handle Class Destructor
Free download pdf