Version 4 of Deleting itcl objects

Updated 2007-02-21 12:59:54

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

  • when deleting an object, other objects may be deleted by its destructor
  • when deleting a class, all inherited classes may be deleted too.

Vincent Wartelle

  #
  # 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
          }
      }
  }

See also incr Tcl


Category Object Orientation Category Itcl