ResetObjResult to call registered free function first time
Closed / Rejected756883ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 21 years ago
Descriptionplain2003-06-18 22:24:41
Hi,
In tclResult.c inside function ResetObjResult(iPtr)
Would you consider
instead of...
if (Tcl_IsShared(objResultPtr)) {
...
} else {
if ((objResultPtr->bytes != NULL)
&& (objResultPtr->bytes !=
tclEmptyStringRep)) {
ckfree((char *) objResultPtr->bytes);
}
objResultPtr->bytes = tclEmptyStringRep;
objResultPtr->length = 0;
if ((objResultPtr->typePtr != NULL)
&& (objResultPtr->typePtr->freeIntRepProc !
= NULL)) {
objResultPtr->typePtr->freeIntRepProc
(objResultPtr);
}
objResultPtr->typePtr = (Tcl_ObjType *) NULL;
}
consider...
if (Tcl_IsShared(objResultPtr)) {
...
} else {
TclDecrRefCount(objResultPtr);
/* This automatically
calls the free function if result object
holds its own internal representation.
And depending on the free function of the
registered type
It might already invalidate the 'bytes' field of
the Tcl_Obj
and set the 'bytes' to NULL and
the 'length' to zero.
*/
if ((objResultPtr->bytes != NULL)
&& (objResultPtr->bytes !=
tclEmptyStringRep)) {
ckfree((char *) objResultPtr->bytes);
}
objResultPtr->bytes = tclEmptyStringRep;
objResultPtr->length = 0;
objResultPtr->typePtr = (Tcl_ObjType *) NULL;
Tcl_IncrRefCount(objResultPtr);
}
I'm not sure if it's a bug, but with the current
implementation, the registered free function is called
when refCount is still one.
Moreover, if the result object holds its internal
representation, the bytes field may hold ths string that
is obtained from calling Tcl_UpdateStringObj, which calls
the registered UpdateStringProc.
The software client is free to use its own memory
allocator when updating the bytes string. So I think it's
a bug to assume that the bytes is allocated using
ckalloc.
-- Yohan
jenglishplain2003-09-06 08:58:53
Logged In: YES user_id=68433 > The software client is free to use its own memory allocator > when updating the bytes string This is incorrect: according to Tcl_RegisterObjType(3), the 'updateStringProc' method *must* use Tcl_Alloc or ckalloc to allocate storage for the byte array.
ysutjandraplain2003-09-09 00:17:52
Logged In: YES user_id=799156 You're right... we use Tcl_Alloc to allocate space for string bytes. But still please consider using Tcl_DecrRefCount and Tcl_IncrRefCount instead of calling the free function directly. The reason is that my program flags an error if the free function is called when refCount is still greater than zero. This flag is useful because a specific object type that we implement will not be able to be recreated once it is deleted. So the flag is to notify users that the current object held by a tcl variable has been freed and they should recreate the object themselves. -- Yohan
msoferplain2005-11-26 17:49:54
Logged In: YES user_id=148712 The freeProc for any Tcl_Obj type may be called with any value of refCount - it will happen if the Tcl_Obj shimmers. A Tcl_Obj's internal rep must be able to be reconstructed from the string representation, by design.
Log in (anonymous is fine) to add a comment.