Memory leak in "info exists array()"
Closed / Fixed219213ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 05:03:39
OriginalBugID: 3851 Bug
Version: 8.2.2
SubmitDate: '1999-12-10'
LastModified: '1999-12-11'
Severity: MED
Status: Assigned
Submitter: techsupp
ChangedBy: hobbs
OS: All
FixedDate: '2000-10-25'
ClosedDate: '2000-10-25'
Name: Edwin Diekemper
ReproducibleScript:
% proc testleak {} {
set foo(1) one
set foo(2) two
for { set idx 0 } { $idx < 200000 } { incr idx } {
info exists foo($idx)
}
}
% testleak
ObservedBehavior:
The size of the process gets very large.
$ ps -p 6804
PID TTY TIME COMD
6804 pts/20 0:05 tclsh
Before running testleak:
$ ls -l /proc/6804
-rw------- 1 user banknet 1622016 Dec 10 14:07 /proc/6804
After running testleak:
$ ls -l /proc/6804
-rw------- 1 user banknet 15613952 Dec 10 14:07 /proc/6804
DesiredBehavior:
Very little change in the size of the process.
I've verified that the growth occurs, but when I run this through
purify, it doesn't complain about any memory leaks.
It only occurs when the array variable on which you are
doing 'info exists' already occurs.
-- 12/10/1999 hobbs
Found the problem. Each time you do 'info exists' on a known
array, it will create the hash entry for the key item if it
didn't already exist. This will occur just once for each key,
but when you just want to know it exists, that's bad. If you
were to set the var following, then it wouldn't take any more
mem (except for the length of the value), so you only reach
the extreme case of waste in the example above.
The comments in the code justifying this were to use existing
C functions and maintain compatibility with 7.6 behavior. It
looks like the internal C funcs have to be rewritten to prevent
this quasi-leak (really a mem waste, no leak).
-- 12/11/1999 hobbs
hobbsplain2000-11-02 04:16:54
Patch from Mark Patton fixes this problem (put in 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.