[Xotcl] Problem with automatic variable unsetting upon return from an instproc?

Uwe Zdun uwe.zdun at wu-wien.ac.at
Wed Nov 19 18:33:28 CET 2003


Hi!

the -volatile option of new does something like this for local objects in the 
current scope (it uses an "unset" trace set in C). Example:

package require XOTcl
namespace import ::xotcl::*

Class A
A instproc destroy args {
  puts "I'm destroyed: [self]" 
  next
}
A proc x {} {
  A new -volatile
}
A x

Does this help?

Uwe


On Wednesday 19 November 2003 16:53, Jim Russell wrote:
> Uwe:
>
>    Thank you so much for the very lucid explanation.  I was hoping that
> I could use the trace mechanism to implement a garbage collection
> scheme.  I was storing the XOTcl object name in an ordinary Tcl variable
> upon which I placed the unset trace.  When the variable goes out of
> scope, the unset callback is called, and I had hoped to dereference the
> variable to call the XOTcl object's destroy method.  However, this
> doesn't appear to be a good approach.  Do you have any suggestions?
>
> Jim
>
> Uwe Zdun wrote:
> > Jim,
> >
> > at first glance, the behavior seems ok to me. You're setting 2 traces to
> > the local scope variable "f" which get unset in reverse order -> so
> > the two "f" outputs should be ok.
> >
> > The rhs traces "2" and "3" are for the two inner invocations. For the
> > outer invocations, the callframe is deleted before the Trace is executed.
> > The problem here is that Tcl does not print the error that occurs.
> >
> > Consider the following plain Tcl example:
> >
> > proc tracecb { name1 name2 op } {
> >    if {[catch {
> >      upvar $name1 var
> >      puts "tracecb \"$name1\" \"$name2\" \"$op\" \"$var\""
> >    } err]} {
> >      puts $err
> >    }
> > }
> > proc x {a} {
> >
> >   ::trace add variable a [list unset] ::tracecb
> >
> >   if {$a == 0} {return}
> >   incr a -1
> >   x $a
> > }
> >
> > x 4
> >
> > This prints:
> > tracecb "a" "" "unset" "0"
> > tracecb "a" "" "unset" "1"
> > tracecb "a" "" "unset" "2"
> > tracecb "a" "" "unset" "3"
> > can't read "var": no such variable
> >
> > so you cannot rely on the "upvar" in the trace to a local scope variable.
> >
> > You can try to refer to an XOTcl variable (an instance variable)
> > with "[self] trace ..." instead.
> >
> > Uwe
>
> _______________________________________________
> Xotcl mailing list  -  Xotcl at alice.wu-wien.ac.at
> http://alice.wu-wien.ac.at/mailman/listinfo/xotcl

-- 
Uwe Zdun
Department of Information Systems, Vienna University of Economics
Phone: +43 1 313 36 4796, Fax: +43 1 313 36 746
zdun@{xotcl,computer,acm}.org, uwe.zdun at wu-wien.ac.at





More information about the Xotcl mailing list