Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2009-07-15
19:34 • Ticket [219316ffff] Garbage collection of object oriented Tcl commands statu... artifact: cb239ff716 user: ferrieux
16:57 • Ticket [219316ffff]: 4 changes artifact: 8439b561e1 user: dkf
16:49 • Ticket [219316ffff]: 4 changes artifact: 22a474e77e user: ferrieux
16:48 • Ticket [219316ffff]: 4 changes artifact: 9547addfc3 user: ferrieux
14:42 • Ticket [219316ffff]: 4 changes artifact: ad990c904c user: dkf
03:53 • Ticket [219316ffff]: 4 changes artifact: f70e8a332e user: ferrieux
2009-07-13
01:21 • Ticket [219316ffff]: 1 change artifact: ce7ac759b6 user: dkf
2009-03-24
23:24 • Ticket [219316ffff]: 4 changes artifact: b6f2bcf7ab user: dkf
2003-02-22
23:58 • Ticket [219316ffff]: 1 change artifact: 7b60f61c6d user: dkf
2001-04-04
04:50 • Ticket [219316ffff]: 1 change artifact: a430553aeb user: dgp
2001-01-09
21:56 • Ticket [219316ffff]: 4 changes artifact: fca9e98c64 user: dkf
2000-11-08
18:33 • Ticket [219316ffff]: 6 changes artifact: 077d056544 user: dkf
2000-10-26
05:10 • New ticket [219316ffff]. artifact: 84380bd2c1 user: nobody

Garbage collection of object oriented Tcl commands

Open219316ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 17 years ago
Descriptionplain2000-10-26 05:10:37
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?
dkfplain2000-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...
dkfplain2001-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.)
dgp Ticket [219316ffff]: 1 change [History diff]2001-04-04 04:50:30
dkf Ticket [219316ffff]: 1 change [History diff]2003-02-22 23:58:31
dkfplain2009-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.
dkf Ticket [219316ffff]: 1 change [History diff]2009-07-13 01:21:50
ferrieuxplain2009-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 ?
dkfplain2009-07-15 14:42:56
You're not missing anything.
ferrieuxplain2009-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
ferrieuxplain2009-07-15 16:49:39
I forgot the obvious [unset x] right in the middle ;-)
dkfplain2009-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...
ferrieuxplain2009-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...
Log in (anonymous is fine) to add a comment.