[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: thoughts on onCleanup and handle classes
From: |
Jaroslav Hajek |
Subject: |
Re: thoughts on onCleanup and handle classes |
Date: |
Tue, 18 May 2010 14:14:03 +0200 |
On Tue, May 18, 2010 at 9:51 AM, Jaroslav Hajek <address@hidden> wrote:
> hi all,
>
> as many of you know, Matlab since v. 2008 or something implements the
> onCleanup object, that can be used for doing cleanup actions. Although
> Octave's unwind_protect is better for most normal usages, there are
> situations where the object nature of onCleanup would be an advantage,
> primarily if you want to export the cleanup action outside the current
> scope.
>
> Now, in Matlab, I think onCleanup is simply a handle class with a
> destructor, although I can't check. However, handle classes are not
> supported in Octave, and I don't think they will be in near future.
> First of all, we'd need to implement the new-style classes, and that
> alone is a task that nobody just picked up.
> Handle classes bring a lot more additional complexity, with their
> dynamic properties and listeners and destructors. Another big problem
> with handle classes is that simple reference counting is no longer
> enough to avoid memory leaks, a handle-based container can contain
> itself (possibly through other objects), much like in Python, so
> circular references are possible (you can't make a circular reference
> with cells and structs). I do not know how Matlab solves this, because
> it is said there is no garbage collection. If it solves it at all.
> Because of this complexity, it may make sense for Octave to mimick
> certain useful handle classes of Matlab. onCleanup could be an
> instance.
> An existing instance is the ftp object by D.B. (here the handle is
> actually an internal property, ftp.curlhandle).
> Another useful thing would be a file object that can close itself
> automatically. Actually, with onCleanup, one can simulate the
> destructor in a rather neat way:
>
> function fileobj (filename)
> obj.fid = fopen (filename);
> obj.close = onCleanup (@() fclose(obj.fid));
> obj = class (obj, "fileobj");
> endfunction
>
> what do you think? Is it worth implementing onCleanup into Octave in C++?
> some two years back D.B. gave a sketch implementation, but I don't
> think it will work anymore.
> Btw., this assumes my understanding of how onCleanup works is correct.
> Can anyone test the following code in Matlab 2008+ for me?
>
> outer.m:
> function outer ()
> x{1} = inner ('wild');
> disp ('to be');
> x{2} = x{1};
>
> inner.m:
> function x = inner (word)
> c = onCleanup (@() disp (word));
> disp ('born');
> x = c;
>
>
>>> outer
>
>
I'm attaching a proposal for onCleanup implementation. Only the basic
functionality is covered. I don't know how, for example,
loading/saving of onCleanup classes works in Matlab, but I don't think
that is important.
The following now works:
octave:1> c = onCleanup (@() disp ("cleanup"))
c =
onCleanup (@() disp ("cleanup"))
octave:2> clear c
cleanup
octave:3> c = onCleanup (@() disp ("cleanup"))
c =
onCleanup (@() disp ("cleanup"))
octave:4> page_screen_output (0) # without this, the output from
exiting Octave is swallowed by pager.
octave:5> quit()
cleanup
as well as related use in a function
function progress1(n)
old_pso = page_screen_output (false);
c = onCleanup (@() page_screen_output (old_pso));
for i = 1:n
printf ("\r%d", i);
pause (0.25);
endfor
printf ("\rdone.\n");
endfunction
note that although this is less wordy than unwind_protect, you still
need to spell the name twice.
The implementation uses a simple octave_value class, that stores the
handle and calls it when destroyed. Care is needed in the destructor
to handle all exceptions and not disrupt the existing error/interrupt
state.
Note also how onCleanup guards against the infamous crashes resulting
from unloading/reloading the library while objects referencing it
still exist:
octave:1> c = onCleanup (@() disp ("cleanup 1"))
c =
onCleanup (@() disp ("cleanup 1"))
octave:2> system ("touch src/DLD-FUNCTIONS/onCleanup.oct")
ans = 0
octave:3> d = onCleanup (@() disp ("cleanup 2"))
warning: library
/home/hajek/devel/octave/main/src/DLD-FUNCTIONS/onCleanup.oct not
reloaded due to existing references
d =
onCleanup (@() disp ("cleanup 2"))
octave:4> clear onCleanup
octave:5> e = onCleanup (@() disp ("cleanup 3"))
e =
onCleanup (@() disp ("cleanup 3"))
octave:6> clear all
cleanup 1
cleanup 2
cleanup 3
octave:7>
doing this is now as simple as specifying octave_auto_shlib as an
additional inheritance base:
class octave_oncleanup : public octave_base_value, octave_auto_shlib
comments, suggestions? OK to push?
--
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz
oncleanup.diff
Description: Text Data
Re: thoughts on onCleanup and handle classes, David Bateman, 2010/05/18