Tcl Source Code

View Ticket
Login
Ticket UUID: 219316
Title: Garbage collection of object oriented Tcl commands
Type: RFE Created on: 2000-10-26 05:10:37
Submitter: nobody Assigned to: dkf
Subsystem: 47. Bytecode Compiler Severity:
Priority: 8 Last modified: 2009-07-15 19:34:37
Status: Open Closed by:
Resolution: None Closed on:
Version: obsolete: 8.4a3
Description:
OriginalBugID: 3678 RFE
Version: 8.2.1
SubmitDate: '1999-11-23'
LastModified: '2000-04-03'
Severity: LOW
Status: UnAssn
Submitter: techsupp
ChangedBy: hobbs
OS: All
FixedDate: '2000-10-25'
ClosedDate: '2000-10-25'


Name:
Chris Oliver

DesiredBehavior:
Several packages such as TclBlend and TclMico implement a form of
automatic garbage collection for Tcl commands that represent object
instances (Java and CORBA respectively).  However they do this with a
rather gross hack, namely by overwriting the global "cmdName" Tcl
ObjType with their own implementation. The replacement implementation
deletes such a command from the interpreter when the the last Tcl_Obj
that references it is destroyed.

A couple of minor changes to the Tcl core would allow these and other
object oriented Tcl extensions to provide the convenience of automatic
garbage collection to the script writer, without resorting to such
hacking.  I will describe these changes in terms of a possible
implementation, however other ways of achieving the same result are
possible:

1. Add the following new interface:

    TclCommand Tcl_CreateObjCommandEx(Tcl_Interp * interp, char *
cmdName,
                    Tcl_ObjCmdProc * proc, ClientData clientData,
Tcl_CmdDeleteProc * deleteProc, /* boolean*/ int collectMe);

2. Add a corresponding field to struct Command:
    struct Command {
        ...
        int collectMe;
    };

Tcl_CreateObjCommandEx uses the current implementation of
Tcl_CreateObjCommand but also assigns the value of its collectMe
parameter to the collectMe field of the created struct Command.

Tcl_CreateObjCommand can be reimplemented as

    Tcl_CreateObjCommandEx(interp, cmdName, proc, clientData,
deleteProc, 0);

3. Modify `tclExecute.c`FreeCmdNameInternalRep to check the reference
count of the Command struct it points at; if it is about to reach one
(i.e. the only reference will be that of the Namespace hash table) and
its collectMe field is non-zero, then call Tcl_DeleteCommandFromToken on
the Command pointer.

At any rate, the basic idea is that Tcl would ask the command's
implementation if it wants to take action when there are no further
references to it.

Comments?
User Comments:
dkf added on 2000-11-08 18:33:27:
Seems like a reasonable idea to me to add "unnamed" commands that get GCed when their last reference disappears.  It would also be useful in Itcl and many other places.  Support for this could be done instead by making Tcl_CreateObjCommand() produce this sort of command when a NULL or empty string is passed for the command name (currently an error, or at least darned well ought to be.)  This would have the advantage of avoiding the horrible ...Ex function naming convention (which was invented by someone deeply demented; that MS use it a lot doesn't surprise me in the slightest!)

Does this interact with Feather or some of the other ideas being bandied about for 9.0?  I suspect so...

dkf added on 2001-01-09 21:56:52:
Further discussion indicates that for this to be truly useful, great care will need to be taken to ensure that object-representations of these commands don't get lost too easily (Tcl_Objs were designed with the intention that the non-string form be just a cache of what would always be creatable from the string form, and not vice versa.)  This requires doing something much smarter than at present with substitutions into strings (the main way of accidentally losing non-string forms.)

dkf added on 2009-03-24 23:24:08:
Note — mainly so *I* don't forget — long-term want the things returned by TclOO's [$cls new] method to be GCed (unless they get [rename]d). Also see what tdom and tcom do.

ferrieux added on 2009-07-15 03:53:47:
Donal, what I seem to recall from Feather, Mutability, and experience with tcom et al., is that GCing opaque handles is strictly incompatible with EIAS. What am I missing ?

dkf added on 2009-07-15 14:42:56:
You're not missing anything.

ferrieux added on 2009-07-15 16:48:55:
Then what's the plan: 
 (a) reject this request
or
 (b) accept to break EIAS the Feather way, by allowing second-class string-based containers to fail miserably:

      set x [Foo new]
      $x bar
      set mypoorcontainer haha:$x
      set y [lindex [split $mypoorcontainer :] 1]
      $y bar --> BOOM

ferrieux added on 2009-07-15 16:49:39:
I forgot the obvious [unset x] right in the middle ;-)

dkf added on 2009-07-15 16:57:33:
I'm thinking of doing the second. Yes, it's a theoretical problem. No, it's not a problem much in practice. (No, I'm not doing this for 8.6 in any case.)

BTW, your example will break in other ways too; object names as returned by the factory methods are fully qualified. :-) But that's just an unfortunate choice of separator char; a comma would have demonstrated what you were looking to show.

There are third and fourth options too: use explicit refcounting, tie to a scope (or rather to a variable lifetime). There are some interesting ideas here in the wider community (e.g., TclNAP) which is why doing something about this remains a big project; real research is needed...

ferrieux added on 2009-07-15 19:34:37:
OK. But let's prepare to hear again from people we dismissed with a swing of the EIAS club during the NULL cacophony...

About (c) explicit refcounting and (d) var-binding:

To me, (c) is just as manual as hand-destroying, so counts as (a).
And (d) is already what (b) does, but with only a subset of active references (ie not counting references from within other Tcl values), so it will break even more respectable containers like lists and dicts. Eeeek !

As for TclNAP, a cursory examination doesn't cleraly separate it from Feather-like "fragile" values, with just the extra tool of a script-level access to refcount-bumping (which I find rather ugly: better keep true references, so that you can enumerate them). So yet another variant of (b).

Bottom line: you're about to open the Pandora Box. It will work "in practice", but Tcl will lose its soul...