Matlab句柄类的析构函数未按预期工作

问题描述 投票:0回答:1

我正在使用tcpip-protocol的matlab类中工作。在类的早期状态中,它继承了句柄超类,构造函数和析构函数正常工作。因为看不到我的析构函数停止工作,所以我无法清楚地说出问题何时出现。我也不想通过孔类,因为它随着时间的推移而增长。我尝试显示该类中的重要代码段:

classdef wsg50 < handle

    properties (Hidden)[...]

    properties (Constant, Access = private)[...]

    properties (Access = private)[...]

    properties

    methods
        %CONSTRUCTOR
        function obj = wsg50(varargin)

            %Set Standards
            obj.IP = 'localhost';
            obj.PORT = 1000;
            obj.verbose = false;
            obj.debug = false;
            obj.autoopen = false;

            %Set properties
            obj.msg_table = msg_id_tbl();
            obj.buffer = [];

            defaultHost = 'localhost';

            switch nargin
                case 0
                    obj.IP = defaultHost;
                    warning('Host and Port are not set. The object uses defaultHost and defaultPort.')
                case 1
                    obj.IP = varargin{1};
                    warning('Port is not set. The object uses defaultPort.')
                case 2
                    obj.IP = varargin{1};
                    obj.PORT = varargin{2};
                otherwise
                    obj.IP = varargin{1};
                    obj.PORT = varargin{2};
                    obj.check_vars(varargin)
            end

            obj.conf_conn();

            %Setting up Callbackfunction
            obj.TCPIP.BytesAvailableFcnMode = 'byte';
            obj.TCPIP.BytesAvailableFcnCount = 1;
            obj.TCPIP.BytesAvailableFcn = {@obj.TCP_Callback, obj};
            obj.init_b_struct();
            if obj.autoopen
                obj.connect();
                disp('Connection is open!')
            end

            % Cleanup
            obj.cleanup = onCleanup(@()delete(obj));
        end

    end

    methods (Access = private)

        %DESTRUCTER
        function obj = delete(obj)
            if strcmp(obj.TCPIP.Status,'open')
                obj.disconnect
            end
            instrreset
        end
        [...]
    end

    methods (Static)
        %TCP Callback
        %This function will be called if one Byte is available at the TCPIP
        %buffer. 
        function TCP_Callback(tcpsocket,event,obj)
            obj.DataReceive()
            if ~isempty(obj.buffer)
                obj.sort_buffer()
            end
        end
    end

我已经测试了一个简单的处理程序类,该类可以很好地与析构函数配合使用。

最后一件事:我清除类后,将从工作空间中清除变量。因为不会调用delete,所以也不会调用intrreset函数。我意识到我的仪器仍在“仪器控制”应用程序中处于活动状态。如果从那里删除我的工具,则将调用删除的Destructor。

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

EDIT:如果我清除工作区,我在上部使用的短语“停止工作”应该表示不再调用析构函数。

我正在使用tcpip-protocol的matlab类中工作。在类的早期状态中,它继承了句柄超类,构造函数和析构函数正常工作。因为不是...

matlab destructor handle matlab-class
1个回答
0
投票

听起来非常像您在wsg50类中具有Instrument Control App变量引用,在这种情况下,当您从工作空间中清除变量时,因为该变量仍在其他地方被引用,而不是deleted

© www.soinside.com 2019 - 2024. All rights reserved.