Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2015-07-18
05:54 • Closed ticket [a3309d01db]: Leak in array when unsetting keys from a proc pl... artifact: 0bdb482a62 user: dgp
05:52
[a3309d01db] Plug memleak in compiled [unset a($i)]. check-in: 41d3e42dda user: dgp tags: trunk
2015-07-17
19:34 • Ticket [a3309d01db] Leak in array when unsetting keys from a proc status sti... artifact: bb7a4cba67 user: dgp
19:33
[a3309d01db] Test the demonstrates leak in branch off checkin that starts it. check-in: 494aa96a48 user: dgp tags: bug-a3309d01db
2015-07-16
04:03 • Ticket [a3309d01db] Leak in array when unsetting keys from a proc status sti... artifact: e0264a0112 user: bovine
03:22 • Ticket [a3309d01db]: 3 changes artifact: f625c5d424 user: aspect
00:04 • Ticket [a3309d01db]: 4 changes artifact: 3355113d73 user: aspect
2015-07-15
23:59 • Add attachment meminfo.tcl to ticket [a3309d01db] artifact: 05fbf4f240 user: aspect
23:59 • Add attachment memleak-bovine.tcl to ticket [a3309d01dbb0f23c|a3309d01d... artifact: 46a84b1b2d user: aspect
23:10 • Ticket [a3309d01db] Leak in array when using generated lists as keys status ... artifact: 9ee86232c7 user: aku
23:09 • Add attachment gistfile1.tcl to ticket [a3309d01db] artifact: e562b7832c user: aku
23:00 • Add attachment gistfile1.tcl to ticket [a3309d01db] artifact: 002f10dd54 user: msofer
22:50 • New ticket [a3309d01db] Leak in array when using generated lists as keys. artifact: 14ec51bcaf user: bovine

Leak in array when unsetting keys from a proc

Closed / Fixeda3309d01dbb0f23c3cf717ace30d9663fa54735fCreated 11 years agoUpdated 11 years ago
Descriptionplain2015-07-15 22:50:54
I've discovered a memory leak in Tcl 8.6.4 on FreeBSD 9.1 and 10.1 with the following script. The leak does not occur under Tcl 8.5

https://gist.github.com/bovine/15c350d044325a93d5cb -- attached

The issue seems to be specific to using lists that are built up using "lappend". If $foo is using a list that is directly "set" in a single operation, then the leak does not appear to occur.

Additionally, the leak occurs when using "unset" to remove individual elements from myarray. If you "array unset myarray" entirely, then the memory seems to be properly freed.
Referenced by2
akuplain2015-07-15 23:10:10
gist extracted and attached.
aspectplain2015-07-16 00:04:47
Just playing around with this on the script level, I've been able to simplify the test to the attached memleak-bovine.tcl + meminfo.tcl.

Output shows allocated bytes increasing by ~900 each 5 iterations, and (total mallocs-total frees) increasing by 25 - this all after the array is "full" and elements are being deleted.

Note this test doesn't require [lappend].  The dependency on [unset myarray($key)] is genuine -- using [array unset myarray $key] fails to exhibit the leak.

Other variations:

1. moving the entire body of [doit] into the loop at global scope doesn't leak
2. moving the body of [doit] into the loop and wrapping it in a proc leaks
3. making myarray proc-local instead of global leaks
aspectplain2015-07-16 03:22:29
The following patch seems to fix this issue without breaking existing tests.  It's a bit brutish - a more sensitive patch would more closely compare this bytecoded INST_UNSET_ARRAY code with ArrayUnsetCmd().

Bug seems to have existed since 8.6.2, so I guess it's been in the bytecode all along.


Index: generic/tclExecute.c
==================================================================
--- generic/tclExecute.c
+++ generic/tclExecute.c
@@ -4178,11 +4178,11 @@
                 * No nasty traces and element exists, so we can proceed to
                 * unset it. Might still not exist though...
                 */
 
                if (!TclIsVarUndefined(varPtr)) {
-                   TclDecrRefCount(varPtr->value.objPtr);
+                   TclPtrUnsetVar(interp, varPtr, arrayPtr, NULL, part2Ptr, flags, opnd);
                } else if (flags & TCL_LEAVE_ERR_MSG) {
                    goto slowUnsetArray;
                }
                varPtr->value.objPtr = NULL;
                TRACE_APPEND(("OK\n"));
bovine Ticket [a3309d01db]: 4 changes [History diff]2015-07-16 04:03:47
dgpplain2015-07-17 19:34:00
See branch bug-a3309d01db for conventional test that
demos the leak.  Branches off the checkin that bisecting
determines is the cause of the leak.
dgpplain2015-07-18 05:54:17
Fix merged to trunk.
Log in (anonymous is fine) to add a comment.

Attachments: