Version 6 of package forget

Updated 2012-01-06 00:43:34 by RLE

package forget ?package package ...?

Removes all information about each specified package from this interpreter, including information provided by both package ifneeded and package provide.


See also:


RS 2004-02-05: package forget does not remove resources that a package has brought with it. I'm not sure whether it unloads a DLL (8.5 will have unload), but I could verify that it does not remove namespaces created by a package. Now if a package is well-behaved (i.e. creates a single namespace named like itself, in which all variables, commands, and possible child namespaces go), the cleanup could be done with

  namespace delete ::pkgName

Unloading could be combined with this:

 namespace eval ::pkgName {
   set unload {}
   trace var ::pkgName::unload u {unload pkgName[info sharedlib] ;#}
 }

Is that so?


schlenk 2004-02-05: Sorry to say that, but that is only the case for simple packages. Interestingly binary packages are probably a bit better at cleanup, as Tcl_CreateObjCmd has a delete callback slot, so if the command gets deleted by the namespace delete, it would have a chance to cleanup afterwards.

Only if the package author has some precautions in place, like this:

 namespace eval ::foo {
    variable cleanup 0

    proc cleanup {} {
      # do cleanup
    }
    trace add variable ::foo::cleanup unset ::foo::cleanup
 }

This is kind of a hack to detect namespace deletion, but it actually works (which surprised me, since the doc for unset traces says the var is already gone, so i assumed the namespace was already gone too).