Re: c-extension question



Cristian wrote:
....
pObjRet = Tcl_GetObjResult (interp);
Tcl_SetStringObj (pObjRet, res, -1);
return TCL_OK;
....

Don't do this. You should create a new object to return rather than trying to mutate the current interpreter result (which may be shared):

pObjRet = Tcl_NewStringObj(res, -1);
Tcl_SetObjResult(interp, pObjRet);
return TCL_OK;

You should be able to remove the strdup now. The problem is that Tcl_GetObjResult returns whatever the result of the last command was. In your case, the last command was [set a "..."], so the result was that value (which also happens to be your input). You then fetch this result and mutate it to the new value. Because this Tcl_Obj is also stored in $a, then the value of that variable is also updated. Therefore, when you next call the command with $a you get the wrong value.

You should never call any Tcl_Set* function on a Tcl_Obj that you don't "own" (i.e., have created yourself, or otherwise know is unshared). Tcl should panic (terminate) if it detects code trying to modify a shared object -- was the segfault you mentioned actually a panic?

-- Neil
.