0

我正在研究一个 MATLAB 类,该类存储一个使用创建的接口对象,tcpip并包括一个供接口对象使用的回调函数,如下例所示:

classdef wsg50_mini2 < handle

    properties
        TCPIP
    end

    %PUBLIC METHODS
    methods

        %CONSTRUCTOR
        function obj = wsg50_mini2(varargin)
            fprintf('################# I am created #################\n')

            obj.TCPIP = tcpip('localhost',1000);
            obj.TCPIP.OutputBufferSize = 3000;
            obj.TCPIP.InputBufferSize = 3000;
            obj.TCPIP.ByteOrder = 'littleEndian';
            obj.TCPIP.Timeout = 1;

            %Setting up Callbackfunction
            obj.TCPIP.BytesAvailableFcnMode = 'byte';
            obj.TCPIP.BytesAvailableFcnCount = 1;
            obj.TCPIP.BytesAvailableFcn = {@obj.TCP_Callback, obj};
        end
    end

    %PRIVATE METHODS
    methods (Access = private)

        %DESTRUCTOR
        function delete(obj)
            fprintf('################# Hey I am called! #################\n')
            instrreset
        end
    end

    %STATIC METHODS
    methods (Static)
        %TCP Callback
        %This function will be called if one Byte is available at the TCPIP
        %buffer.
        function TCP_Callback(tcpsocket,event,obj)
            fprintf('Loading 1 Byte Data From Buffer.\n')
        end
    end
end

当我清除我的类时,变量将从工作区中清除,但delete不会调用析构函数。为什么不?

我意识到,我的仪器在仪器控制应用程序中仍然处于活动状态。如果我从那里删除我的仪器,我的delete析构函数就会被调用。

我认为这是 tcpip 类的一些奇怪行为。

4

2 回答 2

2

听起来很像您的类中有wsg50变量引用Instrument Control App,在这种情况下,当您从工作区中清除变量时,因为它仍然在其他地方引用它不是deleted.

让我们看一个简单的例子:

classdef myClass < handle
  properties
    name = '';
  end
  methods
    function delete ( obj )
      fprintf ( '%s being deleted\n', obj.name );
    end
  end
end

好吧,让我们运行一些代码:

var = basicClass;
var.name = '123';

如果我们清除这个变量,那么你可以看到delete被调用:

>> clear var
 being deleted

如果我们重新运行此代码并在其他地方进行引用:

var = basicClass;
var.name = '123';
otherReference.var = var;

查看两个变量,它们是相同的(如预期的那样):

>> var    
var = 
  myClass with properties:
    name: '123'

>> otherReference.var
ans = 
  myClass with properties:
    name: '123'

那么如果我们clear var看一下其他参考资料会发生什么

clear var
otherReference.var.name

>> otherReference.var
ans = 
  myClass with properties:
    name: '123'

我们可以看到类变量是活动的,因为这与作为函数输入的类没有区别,并且当该函数完成时,该变量的本地副本从作用域中清除。

如果我们真的想删除变量,那么您可以执行以下操作,在其中显式运行析构函数方法:

var = basicClass;
var.name = '123';
otherReference.var = var;
delete(var);
otherReference.var.name

delete行给我们:

123 being deleted

如果我们看一下,otherReference.var我们会得到:

Invalid or deleted object.

这实际上会删除变量,您会看到otherReference现在是指向无效句柄的指针。

于 2020-01-21T15:58:49.217 回答
0

问题是线路obj.TCPIP.BytesAvailableFcn = {@obj.TCP_Callback, obj};。该引用@obj.TCP_Callback在 tcpip 对象中创建了该类的一个实例,这是该类本身的一个属性。

最简单的解决方案是清除obj.TCPIP.BytesAvailableFcn = ''析构函数内部并手动调用析构函数。如果代码仅由其创建者使用,这是可以的。

为了保持面向对象的代码完整,包装类适用于回调函数。主文件使用 Wrapper Class 和 Listener 进行更新,如下所示。回调的设置被替换为两者的组合。然后,析构函数需要调用包装器的析构函数(从回调中删除自身的实例)和侦听器的析构函数:

classdef wsg50_mini2 < handle

    properties
        TCPIP
        TCPIPWrapper
        LH
    end

    %PUBLIC METHODS
    methods

        %CONSTRUCTOR
        function obj = wsg50_mini2(varargin)
            fprintf('################# I am created #################\n')

            % Create TCPIP Object
            obj.TCPIP = tcpip('localhost',1000);

            % Create TCPIP Wrapper
            obj.TCPIPWrapper = TCPIPWrapper(obj.TCPIP)

            % Add Listener
            obj.LH = listener(obj.TCPIPWrapper,'BytesAvailableFcn',@obj.onBytes);

            obj.TCPIP.OutputBufferSize = 3000;
            obj.TCPIP.InputBufferSize = 3000;
            obj.TCPIP.ByteOrder = 'littleEndian';
            obj.TCPIP.Timeout = 1;

        end
    end

    %PRIVATE METHODS
    methods (Access = private)

        %DESTRUCTOR
        function delete(obj)
            fprintf('################# Hey I am called! #################\n')
            % Destroy Listener
            delete(obj.LH);
            % destroy Wrapper
            delete(obj.TCPIPWrapper);

            instrreset
        end
    end

    %STATIC METHODS
    methods (Private)
        function onBytes(obj,~,~)
            fprintf('Loading 1 Byte Data From Buffer.\n')
        end
    end
end

额外的 Wrapper 如下所示:

classdef TCPIPWrapper < handle
    properties(Access=private)
        tcpip
    end
    events
        BytesAvailableFcn
    end
    methods
        % tcpipCallbackWrapper Constructor
        function obj = tcpipCallbackWrapper(tcpip)
            disp 'CONSTRUCTED tcpipCallbackWrapper'
            % Keep a reference to the tcpip object
            obj.tcpip = tcpip;
            % Adding this listener will result in the destructor not being
            % called automatically anymore; but luckily we have myClass
            % which will explicitly call it for us
            obj.tcpip.BytesAvailableFcnMode = 'byte';
            obj.tcpip.BytesAvailableFcnCount = 1;
            obj.tcpip.BytesAvailableFcn = @obj.bytesAvailableFcn;
        end
        % tcpipCallbackWrapper Destructor
        function delete(obj)
            disp 'DESTRUCTED tcpipCallbackWrapper'
            % Overwrite the callback with a new empty one, removing the
            % reference from the callback to our class instance, allowing
            % the instance to truly be destroyed
            obj.tcpip.BytesAvailableFcn = '';
        end
    end
    methods (Access=private)
        % Callback for BytesAvailableFcn
        function bytesAvailableFcn(obj,~,~)
            notify(obj, 'BytesAvailableFcn');
        end
    end
end
于 2020-05-27T12:19:57.383 回答