Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 3cd2aef90b4a2143d7adf834134fca06ebef4136
Ticket: 3598875fffffffffffffffffffffffffffffffff
new macro TclFreeIfRefCountZero
User & Date: nijtmans on 2013-01-03 15:38:09
Changes

  1. icomment:
    > Code written for one pattern cannot be converted to the other
    > by macro
    Let's record here why not. Dividing the API in "comsumers"
    (like Tcl_SetObjResult, Tcl_ListObjAppendObj,
    Tcl_SetVar2Ex) and "producers" (like Tcl_NewIntObj,
    Tcl_GetVar2Ex, Tcl_GetObjResult), let's assume we
    change the contract: "producers" will either return
    a new object with refcount 1, either increment the
    refcount of an already existing object and return that.
    "consumers" no longer increment the refcount from
    the object they receive. That would pretty well
    keep existing extension code unchanged... Is it?
    
    One example
        Tcl_Obj *resultObj = Tcl_NewIntObj(123);
        Tcl_IncrRefCount(resultObj);
        printf("%s", Tcl_GetString(resultObj));
        Tcl_DecrRefCount(resultObj);
    This code works fine in Tcl8, but would lead
    to a memory leak in Tcl9. Remove the
    Tcl_IncrRefCount, and it will work in both.
    
        Tcl_Obj *resultObj = Tcl_NewIntObj(123);
        Tcl_SetVar2Ex(interp, ......, resultObj);
        Tcl_SetObjResult(interp, resultObj);
    This works in Tcl8, but with a changed contract
    an additional Tcl_IncrRefCount should be done.
    It's a pretty common pattern: returning the same
    object that is operated on. Or, maybe, a
    TCL_NEWREF flag could be added
    to Tcl_SetVar2Ex, which makes it do the increment
    as before. Then - at least - code can be written with
    is usable in both Tcl8 and Tcl9. But where is the gain?
    
    However - still - the gain is questionable here, I agree
    with dkf on that. I could imagine that Tcl one day is
    rewritten to let Tcl_Obj's be shared among threads, then
    having a refcount of 0 is dangerous, any thread could
    clean it up at will, that could be a good reason for
    changing the contract as suggested.
    Or maybe when Tcl switches to use garbage collection in
    stead of refcounting. But for Tcl9 I don't see that happening,
    because it has a lot of other dangerous implications. And
    when it happens, the contract can be changed along with it.
    
    
  2. login: "nijtmans"
  3. mimetype: "text/plain"
  4. username: "nijtmans"