• (I suspect) basic question about a C-language extension

    From Alan Grunwald@nospam.nurdglaw@gmail.com to comp.lang.tcl on Mon May 4 18:10:08 2026
    From Newsgroup: comp.lang.tcl

    Hi,

    I'm messing about with one of the above beasts for the first time.

    Please will someone provide code fragments to return the string "one",
    and one to return the list {"one" "two"}.

    I'm concerned that I don't properly understand how reference counting
    works, nor indeed how I would notice if I've got it wrong.

    Do I need more than the following?

    a)

    obj = Tcl_NewStringObj ("one", 3);
    Tcl_SetObjResult (obj);
    return TCL_OK

    b)

    obj = Tcl_NewListObj (0, nullptr);
    Tcl_ListObjAppendElement (interp, obj, Tcl_NewStringObj ("one", 3);
    Tcl_ListObjAppendElement (interp, obj, Tcl_NewStringObj ("two", 3);
    Tcl_setObjResult (obj);
    return TCL_OK;


    Many thanks,



    Alan
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Mon May 4 19:38:37 2026
    From Newsgroup: comp.lang.tcl

    * Alan Grunwald <nospam.nurdglaw@gmail.com>
    | Please will someone provide code fragments to return the string "one",
    | and one to return the list {"one" "two"}.

    | I'm concerned that I don't properly understand how reference counting
    | works, nor indeed how I would notice if I've got it wrong.

    | Do I need more than the following?

    No, modulo the obvious syntax errors in the C code (missing interp in Tcl_SetObjResult(), missing closing paren for Tcl_ListObjAppendElement :-)

    | a)

    | obj = Tcl_NewStringObj ("one", 3);

    reference count of obj is 0

    | Tcl_SetObjResult (obj);

    reference count of obj is 1. Whenever the interps' result is reset, the refcount of the current result (=obj) is 'decr', so it drops to zero and
    the obj is freed.


    | b)

    | obj = Tcl_NewListObj (0, nullptr);

    reference count of obj is 0

    | Tcl_ListObjAppendElement (interp, obj, Tcl_NewStringObj ("one", 3);

    reference count of obj is 0, refcount of the 'anonymous' Tcl_NewStringObj("one", 3) is 1.

    | Tcl_ListObjAppendElement (interp, obj, Tcl_NewStringObj ("two", 3);

    reference count of obj is 0, refcount of the 'anonymous' Tcl_NewStringObj("two", 3) is 1.

    | Tcl_setObjResult (obj);

    reference count of obj is 1, refcount of the list objs stay 1.

    Whenever the interps' result is reset, the refcount of the current
    result (=obj) is 'decr', so it drops to zero and the obj is freed.
    Freeing the list also decrs the refcount of each list element, which
    then also drop to 0 and are freed.



    Note that whenever you pass a newly created obj to some function which
    might incr/decr the refcount, you need to incr/decr the refcount around
    the call yourself:


    void SomeFunction(TclObj *obj) {
    Tcl_IncrRefCount(obj);
    // ... do some stuff with obj ...
    Tcl_DecrRefCount(obj);
    }

    TclObj *newobj = Tcl_NewStringObj("foo",3);
    SomeFunction(newobj);
    // newobj is now no longer 'alive' here
    Tcl_SetObjResult(interp, newobj); // invalid!

    TclObj *newobj = Tcl_NewStringObj("foo",3);
    Tcl_IncrRefcount(newobj);
    SomeFunction(newobj);
    Tcl_SetObjResult(interp, newobj);
    Tcl_DecrRefcount(newobj); // only *after* Tcl_SetObjResult!


    HTH
    R'
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Alan Grunwald@nospam.nurdglaw@gmail.com to comp.lang.tcl on Mon May 4 23:45:40 2026
    From Newsgroup: comp.lang.tcl

    On 04/05/2026 18:38, Ralf Fassel wrote:
    * Alan Grunwald <nospam.nurdglaw@gmail.com>
    | Please will someone provide code fragments to return the string "one",
    | and one to return the list {"one" "two"}.

    | I'm concerned that I don't properly understand how reference counting
    | works, nor indeed how I would notice if I've got it wrong.

    | Do I need more than the following?

    No, modulo the obvious syntax errors in the C code (missing interp in Tcl_SetObjResult(), missing closing paren for Tcl_ListObjAppendElement :-)


    Thanks Ralf!

    --- Synchronet 3.22a-Linux NewsLink 1.2