[Xotcl] results of the poll
Gustaf Neumann
neumann at wu-wien.ac.at
Mon Mar 4 18:47:47 CET 2002
On Monday 04 March 2002 18:21, Zoran Vasiljevic wrote:
> > suggestion implemented in C. Actually there is no need
> > in general for using tcl-variables to implement volatile
> > objects (we could handle this on a pop of stack frames),
> > but at least for pure tcl-procs, var traces are the simplest
> > implementation. The current implementation (purely in C)
>
> It is not clear: does the C implementation use internal stack
> for garbage collection or the Tcl-trace variable mechansim?
the var-trace mechanism
> It is important to know when doing (or not doing!) such
> constructs:
>
> Class foo
> foo instproc test args {}
>
> [foo new -volatile] test ; # object leak ?
this sets as a side effect a tcl variable in the current
scope. if the scope is e.g. a tcl proc, it will be
automatially unset, when the scope is left.
> In case of volatile implementation with var traces,
> one should do:
>
> set bar [foo new -volatile]
> $bar test
Note, that this is essentially the same as
above. the call of [foo new] cannot set a
vartrace on the "bar" variable. The result
of the "new" is a tcl_obj, and not a variable.
the tcl_obj is bound to the variable bar...
so, the magic of the object deletion happens
via another variable, that is set as a side
effect. these magic variables are named similarly
to the object name: in the example
set bar [foo new -volatile]
the objname will be ::xotcl::__#1 (or ...__#2, etc),
the magic variable is __#1. therefore, one can
refer from the object name to the variable name via
set vn [namespace tail $bar]
unset $vn
will trigger the deletion of the object
created with new. There is a potential danger
of name conflics, but if one follows the rule:
don't start user-variable names with "__",
everyting will be safe.
I have used this example with the manual unset
for explanatory purposes. Under most conditions you will
not need to unset the magic variable by hand....
hope that this explains the details.
best regrads
-gustaf
More information about the Xotcl
mailing list