TIP 550: Garbage Collection for TclOO

EuroTcl/OpenACS 11 - 12 JULY 2024, VIENNA
Author:         Donal K. Fellows <[email protected]>
State:          Draft
Type:           Project
Vote:           Pending
Created:        11-Jun-2019
Tcl-Version:    9.1
Keywords:       Tcl, memory


This TIP changes the semantics of Tcl and TclOO so that objects can be reclaimed automatically when they are no longer referenced.


One of the use patterns that people use TclOO for is to create relatively short-lived objects that manage other things. Currently doing this requires remembering to explicitly delete the objects once they are no longer needed, usually by calling their destroy method, but this is comparatively onerous. If we instead adopt a garbage collection mechanism, we can move instead to deleting objects once they are no longer referenced by the rest of the program; this is a reasonable fit for objects whose name is not predetermined, as their name needs to be saved into a variable (or put inside a list or other data-containing value) already in order for them to be usable.


When the new method of oo::class is called (and hence by default for any class that doesn't override it), it picks a new unused name currently. This behaviour shall be extended so that when the final reference from Tcl code to that name is lost (which shall be determined through mechanisms such as reference counting) the object will be deleted.

Each use of self object (or its common alias self) within the object shall create another reference to the object. Looking it up through mechanisms such as info class instances shall create another reference to the object (while it exists).

Renaming a garbage-collectable object (to any name) stops it from being garbage collected, just as if it had been manufactured with create. Objects with explicit names (which includes all classes themselves under normal circumstances) are assumed to be not garbage collected, as the names could be used in source code directly, without having been obtained from a creation method.

Instances may also be explicitly deleted through all existing mechanisms. After that, those existing references will be treated as if they were plain strings.


To be done.


This document is placed in public domain.