MATLAB - the destructor object does not work when listeners are involved

I have two classes: Plantand Generator. Generatorcreates a vector and passes it through notify(), which Plantlistens. The following are the classes. Please note that I did not include the actual data generation method because it is not relevant to my question.

classdef Plant < handle
    properties
         Listener
    end
    methods
         function ListenerCallback(obj, data)
             #% Perform an operation on data
         end
    end
end

classdef Generator < handle
    properties
        plant
    end
    events
        newSignal
    end
    methods
        function obj = Generator(plant)
            obj.plant = plant;
            obj.plant.Listener = addlistener(obj, 'newSignal', ...
                @(src, data) obj.plant.ListenerCallback(data));
        end
        function delete(obj)
            delete(obj.plant.Listener);
            disp('Generator instance deleted');
        end
    end
end

I noticed that the destructor Generatorbehaves very strange: the first time you create it, then delete the instance Generator, it does not start the destructor until the second time I create the instance Generator. Here is an example:

>> P = Plant
P = 
  Plant handle

  Properties:
    Listener: []
  Methods, Events, Superclasses

>> G = Generator(P)
G = 
  Generator handle

  Properties:
    plant: [1x1 Plant]
  Methods, Events, Superclasses
>> clear G #% DESTRUCTOR NOT CALLED??
>> G = Generator(P)
Generator instance deleted #% why is the destructor run now?
G = 
  Generator handle

  Properties:
    plant: [1x1 Plant]
  Methods, Events, Superclasses
>> clear G
Generator instance deleted #% and why is the destructor run properly now?

, . , ? ( Plant.ListenerCallback() Generator, .)

EDIT. , clear G, G , Generator P.Listener.Source. . P.Listener, G.. , , ?

+2
2

?

clear G #% DESTRUCTOR NOT CALLED??

P

G
G = Generator(P)
Generator instance deleted #% why is the destructor run now?

Generator, . Generator, .

G = 
  Generator handle

  Properties:
    plant: [1x1 Plant]
  Methods, Events, Superclasses
>> clear G
Generator instance deleted #% and why is the destructor run properly now?

, : (1) plant Generator. (2) Generator. (3) plant (!!!) (4) G , Generator. , clear G .


, clear delete, - clear

function clear(varargin)

%# check for Generator objects
isGenerator = cellfun(@(x)evalin('caller','isa(x,''Generator'');'),varargin);

%# I loop here b/c I don't have the time to carefully construct
%# the evalin commands
%# Generator gets deleted, everybody else gets cleared
for i=1:nargin
   if isGenerator(i)
      evalin('caller',sprintf('delete %s',varargin{i}));
   else
      evalin('caller',sprintf('builtin(''clear'',''%s'');',varargin{i});
   end
end
+1

, 2- , ...

Matlab ; , . :

 obj.plant.Listener = addlistener(obj, 'newSignal', ...
                @(src, data) obj.plant.ListenerCallback(data));

obj. obj (, ), . :

 obj.plant.Listener = addlistener(obj, 'newSignal', ...
            @(src, data) src.plant.ListenerCallback(data));

. - , , " ", hardcoding .

, - - !

+1

Source: https://habr.com/ru/post/1544364/


All Articles