Tcl dumps when creating string rep for custom type

Tcl dumps when creating string rep for custom type

Post by Anders Mo » Sat, 22 Oct 2005 19:07:18

Hi all

I've registered my own type using Tcl_RegisterObjType, and created a
command for creating objects of this type.

This works fine, EXCEPT when a string representation has been assigned
to the object. Then the code dumps when re-assigning a value :

set g [test] ; # test is the command used to create the object
puts $g ; # OK - creates a string rep, works fine
set g oo ; # DUMPS

If no string rep is assigned in the C code, no dump takes place.

Everything takes place in the snippet below - if someone could take a
quick for any obvious mistakes I would be grateful.

Regards, Anders Moe.


void dup_tcl_obj (Tcl_Obj* src, Tcl_Obj* copy)


void update_string_repr (Tcl_Obj* obj)

char* c = "test string\0";
obj->bytes = c;
obj->length = strlen (c);

int set_from_any_proc (Tcl_Interp* interp, Tcl_Obj* o)
return TCL_OK;

// Type spec

Tcl_ObjType obj_type = {
0, /* &free_tcl_obj, */

// Callback for "test" command

int gcTclTest (ClientData clientData, Tcl_Interp* interp, int objc,
Tcl_Obj *CONST objv[])
static bool type_created = false;
if (!type_created) {
Tcl_RegisterObjType (&obj_type);
type_created = true;

// Create Tcl object
Tcl_Obj* obj = Tcl_NewObj();
Tcl_InvalidateStringRep (obj); // Force call to updates_string_repr

obj->typePtr = &obj_type;

Tcl_SetObjResult (interp, obj);
return TCL_OK;


Tcl dumps when creating string rep for custom type

Post by Don Porte » Sat, 22 Oct 2005 21:43:40


Let's review the Tcl_RegisterObjType documentation:

The updateStringProc member...
... Storage for the byte array must be allocated in
the heap by Tcl_Alloc or ckalloc. ...

Note that your code does not do what the docs require.

| Don Porter Mathematical and Computational Sciences Division |
| XXXX@XXXXX.COM Information Technology Laboratory |
| ~DPorter/ NIST |