这与this question有些相关,但不完全相关。
我有两个班级,FunctionWrapper
并且TimerWrapper
:
classdef FunctionWrapper < handle
methods
function Fcn(obj)
disp('FunctionWrapper.Fcn was called!');
end
end
end
classdef TimerWrapper < handle
properties
Timer
end
methods
function obj = TimerWrapper(other_object)
obj.Timer = timer;
set(obj.Timer, 'Period', 1);
set(obj.Timer, 'ExecutionMode', 'fixedSpacing');
set(obj.Timer, 'TimerFcn', @(event, data) other_object.Fcn);
end
function start(obj)
start(obj.Timer);
end
function stop(obj)
stop(obj.Timer);
end
function delete(obj)
disp('destructor called!');
delete(obj.Timer);
end
end
end
假设我在命令行窗口中执行以下代码:
>> F = FunctionWrapper;
>> T = TimerWrapper(F);
>> clear T %# T's destructor not called
>> timerfind %# just to verify that no, the destructor was never called
Timer Object: timer-1
Timer Settings
ExecutionMode: fixedSpacing
Period: 1
BusyMode: drop
Running: off
Callbacks
TimerFcn: @(event,data)other_object.Fcn
ErrorFcn: ''
StartFcn: ''
StopFcn: ''
这里发生了什么?我知道timer
需要手动删除对象,但我认为这将在析构函数中处理TimerWrapper
。如果不使用Amro 丑陋但直接的解决方法来重载clear
命令,有没有办法clear T
从工作区中取出?此外,什么都没有引用T
,那么为什么存在对它的引用呢?(析构函数从未被调用的事实暗示了这一事实。)这是否隐藏在计时器对象本身中?