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

Gustaf Neumann neumann at wu-wien.ac.at
Wed Nov 19 18:55:51 CET 2003


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, i have simplified your example by deleting the trace calls and your 
replacemented of "new" and added simply "-volatile".  When an object 
is created with "new -volatile" it will be destroyed automatically, 
when the current tcl-proc/object-proc/instproc is left. 

So, after main is processed, all instances of factorial are deleted. 

Was this, what you were looking for?

#########################################
xotcl::Class factorial
factorial instproc compute { rhs } {
     puts "rhs = $rhs"
     if { $rhs > 1 } {
         set f [ factorial new -volatile ]
         set lhs [ $f compute [ expr $rhs - 1 ] ]
     } else {
         set lhs 1
     }
     set product [ expr $lhs * $rhs ]
     return $product
}

proc main { value } {
     set f [ factorial new -volatile]
     puts "${value}! = [ $f compute $value ]"
}

main [expr [ llength $argv ] ? [ lindex $argv 0 ] + 0 : 3 ]
################################################


 Another simple approach for some kind of "garbarge collection" in xotcl
 is to put your temporary objects into a container and delete that when
 appropriate.

 best regards
-gustaf

-- 
Univ.Prof. Dr.Gustaf Neumann
Abteilung für Wirtschaftsinformatik
WU-Wien, Augasse 2-6, 1090 Wien



More information about the Xotcl mailing list