Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Memory leak after Tcl_InvalidateStringRep

Closed / Wont Fix219397ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 25 years ago
Descriptionplain2000-10-26 05:12:24
OriginalBugID: 6226 Bug
Version: 8.3.2
SubmitDate: '2000-09-07'
LastModified: '2000-09-08'
Severity: MED
Status: UnAssn
Submitter: techsupp
ChangedBy: ericm
OS: Solaris
OSVersion: 5.7
Machine: sun4u sparc SUNW,Ultra-5_10
FixedDate: '2000-10-25'
ClosedDate: '2000-10-25'


Name:
Fabrice Pardo

ReproducibleScript:
1) add lines to tclTest.c (see below)
2) change MEM_DEBUG_FLAGS option in Makefile to -DTCL_MEM_DEBUG
3) do a "make clean; make runtest"

    % history keep 0
    % tolist {x  y} 1 ; memory info ;# repeat, "current packets allocated" is growing
    % tolist {x y} 1  ; memory info ;# repeat, "current packets allocated" is not growing

########################
lines added to tclTest.c
########################

...
static intTesttolistObjCmd _ANSI_ARGS_((ClientData dummy,
    Tcl_Interp *interp, int objc,
    Tcl_Obj *CONST objv[]));
...

/*----------------------------------------------------------------------
 *
 * TesttolistObjCmd --
 *
 *      This function implements a "tolist _list_ _invalStringFlag_"
 *      command, similar to
 *      "lrange _list_ 0 end" command when _invalStringFlag_ == 0,
 *      with an added call to Tcl_InvalidateStringRep(_list_)
 *      when _invalStringFlag_ == 1
 *
 * Results:
 *      The _list_ argument
 *
 * Side effects:
 *      # take care on 1 or 2 spaces between x and y.
 *      tolist {x  y} 1 ;# undesireable memory grown in interactive mode
 *      tolist {x y} 1  ;# no side effect
 *      tolist {x  t} 0 ;# no side effect
 *      set l {x  y}
 *      tolist $x 1     ;# no side effect
 */

int
TesttolistObjCmd(notUsed, interp, objc, objv)
    ClientData notUsed;/* Not used. */
    Tcl_Interp *interp;/* Current interpreter. */
    int objc;/* Number of arguments. */
    register Tcl_Obj *CONST objv[];/* Argument objects. */
{
    Tcl_Obj *listPtr;
    Tcl_Obj **elemPtrs;
    int listLen, invalStringFlag, result;

    if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "list invalStringFlag");
return TCL_ERROR;
    }

    listPtr = objv[1];
    result = Tcl_ListObjGetElements(interp, listPtr, &listLen, &elemPtrs);
    if (result != TCL_OK) {
return result;
    }

    if (Tcl_GetBooleanFromObj(interp, objv[2], &invalStringFlag) != TCL_OK) {
        return TCL_ERROR;
    }

    if (invalStringFlag) {
        Tcl_InvalidateStringRep(listPtr);
    }

    Tcl_SetListObj(Tcl_GetObjResult(interp), listLen, elemPtrs);
    return TCL_OK;
}

...
    Tcl_CreateObjCommand(interp, "tolist", TesttolistObjCmd, 0, NULL);
...

ObservedBehavior:
If an argument is converted to a list
and if his internal string representation
is invalidated, there is a memory
leak if the internal string representation
was not canonical (e.g. "x  y" instead of "x y").

This is because TclReleaseLiteral procedure
doesnt find the new string internal representation
"x y" in his database.

Is it a compiler bug, or an Object/Tcl_InvalidateStringRep
insufficient documentation ?



Comment from Paul Duffin (via comp.lang.tcl):

There is no memory leak because the string is still reachable from the
literal table. However, there may be a problem if the literal table
can grow forever but it won't be a memory leak.
e.g. if the following code could potentially add a thousand strings
to the literal table (not sure if it does) which is 'leaked' as
far as the application is concerned.

        set string a
        for {set i 0} {$i < 1000} {incr i} {
            eval puts $string
            append string a
        }
 
-- 09/08/2000 ericm
dkfplain2000-11-20 18:33:15
I suspect that for this bug to bite in an application, the contortions you'd be going through to achieve it would be pretty horrible.  Normal code (particularly stuff inside some finite number of procedures, which describes most stuff written) is not going to be hit by this.

It is quite possible that Tcl_Main ought to be passing the TCL_EVAL_DIRECT flag to Tcl_RecordAndEval and that this would in fact fix the problem by inhibiting compilation of code typed in interactively.  But I'm not sure that this would be the right thing to do...
dkf Ticket [219397ffff]: 1 change [History diff]2001-02-08 21:33:50
hobbs Ticket [219397ffff]: 1 change [History diff]2001-08-02 08:41:06
hobbsplain2001-08-07 05:29:12
Logged In: YES 
user_id=72656

This doesn't really seem to be a valid problem unless a 
regular use case can show that it is.
Log in (anonymous is fine) to add a comment.