octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fwd: Re: thoughts on onCleanup and handle classes


From: Kai Habel
Subject: Fwd: Re: thoughts on onCleanup and handle classes
Date: Tue, 18 May 2010 10:20:22 +0200

-------- Original-Nachricht --------
Datum: Tue, 18 May 2010 10:18:24 +0200
Von: "Kai Habel" <address@hidden>
An: Jaroslav Hajek <address@hidden>
Betreff: Re: thoughts on onCleanup and handle classes


-------- Original-Nachricht --------
> Datum: Tue, 18 May 2010 09:51:42 +0200
> Von: Jaroslav Hajek <address@hidden>
> An: octave maintainers list <address@hidden>
> Betreff: thoughts on onCleanup and handle classes

> 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
> 
> 
Hello Jaroslav,

with 2009a I get:

>> outer
born
to be
wild

Kai


reply via email to

[Prev in Thread] Current Thread [Next in Thread]