When sourcing several times a tcl script using itcl, there may be a problem because classes we want to create already exists.
Here is a recipe to allow source and source again of such scripts; the point is that
# # tcl 8.4.1 / itcl3.2 (tested on windows xp) # # how to properly delete objects and classes in itcl # # delete objects and classes foreach objid [find objects] { if { [find objects $objid] != "" } { delete object $objid } } foreach classid [find classes] { # avoid to delete already deleted classes if { [find classes $classid] != "" } { delete class $classid } }
Note that classes containing object references of other classes must delete these references, but always with a check "exists ? okay, delete"
public variable childrenlist; # containing a list of children objects destructor { foreach child $childrenlist { if { [find objects $child] != "" } { delete object $child } } }
GWM a problem that actually happened to me today was that a destructor tried to use a variable which had not been defined - the result was that the destructor failed AND the object being deleted was not deleted - memory leak! Luckily my test for undeleted objects on exit see itcl memory leaks detected the left over objects and warned me (sadly in public).
Is there a 'reference pointer' for tcl/itcl? These are very common in my end applications with 3D Graphics where a single object (such as a cube) may be referenced by several different objects (draw 101 cubes in different places using the same cube geometry). You dont want to delete the referenced object until all the other objects using the object are deleted. Obviously a cube is trivial - imagine 101 tie-fighters or mig-21 CGI objects in a scene - you dont want 101 copies of a complex object that looks the same as another object in memory.
See also incr Tcl