Matlab onCleanup function not executed

后端 未结 2 1993
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 08:09

I ran into the following issue in MATLAB R2013a, which for some reason I do not understand does not call the onCleanup function when in a function a timer (including a Timer

2条回答
  •  执笔经年
    2021-01-13 08:47

    Ouch - this is nasty. It's not a bug, but it's certainly not what you'd expect from the documentation, and it's not what you'd want. Fortunately it's pretty easy to work around.

    Firstly, what's happening?

    Well, onCleanup returns you an onCleanup object. This is an object whose sole purpose is to have a destructor method that is set to your @() disp('function ends'). When the object goes out of scope (which you would expect to be at the end of the function mytest2), it gets deleted, its destructor method executes, and your message gets displayed. That's what you expect, I think.

    But when you create the anonymous function @(o,s)disp(' ... waiting for some time'), and assign it to the TimerFcn of your timer, it takes a copy of the entire current workspace of the function mytest2, including the onCleanup object. The timer is created in the base workspace (not the function workspace), and remains in existence even at the end of the function, along with the onCleanup object, which then never goes out of scope, never gets deleted, its destructor function never runs, and you don't get your message.

    Note that:

    1. If you run a = timerfindall; delete(a); in the base workspace, you will get your message, as you've explicitly deleted the timer along with the onCleanup object.
    2. This behaviour about anonymous functions taking a copy of their entire workspace is fully documented, although you may not be aware of it, and although you clearly don't want it to work like that here. It's nasty.

    Fortunately, it's easy to work around:

    function mytest3(time)
      t = timer();
      setTimerFcn(t)
      myclean = onCleanup(@() disp('function ends'));
      pause(time);
    end
    
    function setTimerFcn(t)
      t.TimerFcn = @(o,s)disp(' ... waiting for some time'); 
    end
    

    Now, when the anonymous function is created it only takes a copy of its local workspace (i.e. from the subfunction setTimerFcn), which does not include the onCleanup object. The onCleanup object goes out of scope at the point you expect it to, and everything's fine.

    Hope that helps!

提交回复
热议问题