Home > Archive > Tcl > October 2005 > Tcl dumps when creating string rep for custom type
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Tcl dumps when creating string rep for custom type
|
|
| Anders Moe 2005-10-21, 7:57 am |
|
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 = {
"mystringtype",
0, /* &free_tcl_obj, */
&dup_tcl_obj,
&update_string_repr,
&set_from_any_proc
};
// 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;
}
| |
| Don Porter 2005-10-21, 7:57 am |
| Anders Moe wrote:
> I've registered my own type using Tcl_RegisterObjType,
....
> 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
....
> void update_string_repr (Tcl_Obj* obj)
> {
> char* c = "test string\0";
> obj->bytes = c;
> obj->length = strlen (c);
> }
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 |
| donald.porter@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|_______________________________________
_______________________________|
|
|
|
|
|