Version 0 of itcl::delete

Updated 2006-07-15 14:23:36

Deletes a class object - see itcl::class for an example. This release the memory held in the class.

Deletion automatically calls the destructor method, where any subsidiary tidying up is performed.

  package require Itcl
  itcl::class helloworld {
    variable next "" ;# to form a linked list of helloworlds
    constructor {} {puts "helloworld object $this has been created"}
    destructor { puts "helloworld object $this deleted"
        if {$next!=""} { itcl::delete object $next } }

    method addtolist {newhw} { if {$next==""} { set next $newhw
        } else { $next addtolist $newhw }
    }
    method list {} { puts "$this has next $next"
        if {$next!=""} { $next list } }
  }
  helloworld h1
  helloworld h2
  helloworld h3
  helloworld h4
  h1 addtolist h2
  h1 addtolist h3
  h1 addtolist h4
  # this will print the entire list from h1 to h4.
  h1 list
  # this deletes h1 then h2 (which deletes h3, which deletes h4)
  itcl::delete object h1  

The last statement deletes all objects as they are in the list. Then h1 h2 h3 and h4 will be undefined, beware. The output from the fragment above is:

  helloworld object ::h1 has been created
  helloworld object ::h2 has been created
  helloworld object ::h3 has been created
  helloworld object ::h4 has been created

  ::h1 has next h2
  ::h2 has next h3
  ::h3 has next h4
  ::h4 has next 

  helloworld object ::h1 deleted
  helloworld object ::h2 deleted
  helloworld object ::h3 deleted
  helloworld object ::h4 deleted