Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2000-11-02
04:18 • Closed ticket [219597ffff]: Memory leak with info exists and arrays plus 7 o... artifact: 7cc84af0ee user: hobbs
2000-10-28
01:53 • New ticket [219597ffff]. artifact: 3b6091586a user: msp

Memory leak with info exists and arrays

Closed / Duplicate219597ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-28 01:53:46
I've noticed some strange memory leaks using
info exists on arrays.The leak is also present in 8.3.1.
I'm running Tcl on Linux. The below scripts demonstrate
the memory leak.

# this leaks big time

proc main {argv} {
    expr {srand([clock clicks])}

    for {set i 0} {$i < 1000} {incr i} {
set n1 [expr {int(rand() * 1000)}]
set n2 [expr {int(rand() * 1000)}]
set x($n1,$n2) 0
    }

    while {1} {
set n1 [expr {int(rand() * 1000)}]
set n2 [expr {int(rand() * 1000)}]

if {[info exists x($n1,$n2)]} {
    incr x($n1,$n2)
}
    }
}

# no leak

proc main2 {argv} {
    expr {srand([clock clicks])}

    for {set i 0} {$i < 1000} {incr i} {
set n1 [expr {int(rand() * 1000)}]
set n2 [expr {int(rand() * 1000)}]
set x($n1,$n2) 0
    }

    while {1} {
set n1 [expr {int(rand() * 1000)}]
set n2 [expr {int(rand() * 1000)}]

catch {
    incr x($n1,$n2)
}
    }
}

# no leak

proc main3 {argv} {
    expr {srand([clock clicks])}

    for {set i 0} {$i < 1000} {incr i} {
set n1 [expr {int(rand() * 1000)}]
set x($n1) 0
    }

    while {1} {
set n1 [expr {int(rand() * 1000)}]

if {[info exists x($n1)]} {
    incr x($n1)
}
    }
}
hobbsplain2000-11-02 04:18:24
Dup of 119213, fixed for 8.4a2.

jupiter [~/cvs/tcl8.4] 191 % cvs diff -bc generic/tclVar.c 
Index: generic/tclVar.c
===================================================================
RCS file: /home/cvs/external/tcl/generic/tclVar.c,v
retrieving revision 1.23
diff -b -c -r1.23 tclVar.c
*** tclVar.c    2000/08/25 20:39:31     1.23
--- tclVar.c    2000/11/01 21:16:47
***************
*** 4863,4869 ****
  {
      Var *varPtr;
      Var *arrayPtr;
-     char *msg;
  
      /*
       * The choice of "create" flag values is delicate here, and
--- 4863,4868 ----
***************
*** 4876,4892 ****
       */
  
      varPtr = TclLookupVar(interp, varName, (char *) NULL,
!             0, "access",
!             /*createPart1*/ 0, /*createPart2*/ 1, &arrayPtr);
      if (varPtr == NULL) {
        return NULL;
      }
!     if ((varPtr != NULL) &&
!           ((varPtr->tracePtr != NULL)
!           || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL)))) {
!       msg = CallTraces((Interp *)interp, arrayPtr, varPtr, varName,
                (char *) NULL, TCL_TRACE_READS);
!       if (msg != NULL) {
            /*
             * If the variable doesn't exist anymore and no-one's using
             * it, then free up the relevant structures and hash table entries.
--- 4875,4892 ----
       */
  
      varPtr = TclLookupVar(interp, varName, (char *) NULL,
!             0, "access", /*createPart1*/ 0, /*createPart2*/ 1, &arrayPtr);
! 
      if (varPtr == NULL) {
        return NULL;
      }
! 
!     if ((varPtr->tracePtr != NULL)
!           || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
!       CallTraces((Interp *)interp, arrayPtr, varPtr, varName,
                (char *) NULL, TCL_TRACE_READS);
!     }
! 
      /*
       * If the variable doesn't exist anymore and no-one's using
       * it, then free up the relevant structures and hash table entries.
***************
*** 4894,4902 ****
  
            if (TclIsVarUndefined(varPtr)) {
                CleanupVar(varPtr, arrayPtr);
-           }
            return NULL;
-       }
      }
      return varPtr;
  }
--- 4894,4901 ----
  
      if (TclIsVarUndefined(varPtr)) {
        CleanupVar(varPtr, arrayPtr);
        return NULL;
      }
+ 
      return varPtr;
  }
Log in (anonymous is fine) to add a comment.