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