Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2002-09-06
07:12 • Closed ticket [604609ffff]: leak in command rename traces plus 9 other chang... artifact: f3649b3770 user: dgp
04:15 • Ticket [604609ffff]: 4 changes artifact: 1cb693254d user: hemanglavana
01:19 • Ticket [604609ffff]: 4 changes artifact: dbb44572df user: msofer
2002-09-05
23:20 • Ticket [604609ffff]: 4 changes artifact: 059db61135 user: hemanglavana
2002-09-04
16:42 • New ticket [604609ffff]. artifact: 46e0b11cbe user: msofer

leak in command rename traces

Closed / Fixed604609ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 24 years ago
Descriptionplain2002-09-04 16:42:01
The following scriptlet leaks two structs: a
TraceCommandInfo and a Command

# distilled from test trace-20.9 
#
# No leak if the trace really renames and does
# not delete

proc foo x {set x}
trace add command foo rename {rename foo "";#}
rename foo bar

Valgrind's complaint:

40 bytes in 1 blocks are definitely lost in loss record
3 of 7
 at 0x400467C4: malloc (vg_clientfuncs.c:100)
 by 0x80F9571: TclpAlloc (./../generic/tclAlloc.c:679)
 by 0x806B501: Tcl_Alloc (./../generic/tclCkalloc.c:1002)
 by 0x807A50B: TclTraceCommandObjCmd
(./../generic/tclCmdMZ.c:3385)
 by 0x80798D6: Tcl_TraceObjCmd
(./../generic/tclCmdMZ.c:2932)
 by 0x8066091: TclEvalObjvInternal
(./../generic/tclBasic.c:3033)

56 bytes in 1 blocks are definitely lost in loss record
4 of 7
 at 0x400467C4: malloc (vg_clientfuncs.c:100)
 by 0x80F9571: TclpAlloc (./../generic/tclAlloc.c:679)
 by 0x806B501: Tcl_Alloc (./../generic/tclCkalloc.c:1002)
 by 0x8064813: Tcl_CreateCommand
(./../generic/tclBasic.c:1512)
 by 0x80CC92E: Tcl_ProcObjCmd (./../generic/tclProc.c:136)
 by 0x8066091: TclEvalObjvInternal
(./../generic/tclBasic.c:3033)
hemanglavanaplain2002-09-05 23:20:29
Logged In: YES 
user_id=81875

The following patch should take care of the first complaint:

Index: tclBasic.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclBasic.c,v
retrieving revision 1.69
diff -r1.69 tclBasic.c
2526,2527c2526,2532
<        * rename traces; while a delete trace is active we
will not
<        * process any more delete traces 
---
>        * rename traces; while if a delete trace is active
we will never
>        * reach here -- because Tcl_DeleteCommandFromToken
checks for the
>        * condition (cmdPtr->flags & CMD_IS_DELETED) and
returns immediately
>          * when a command deletion is in progress. For all
other traces,
>          * delete traces will not be invoked but a call to
TraceCommandProc
>          * will ensure that tracePtr->clientData is freed
whenever the
>          * command is "oldName" deleted.
2531,2533d2535
<       }
<       if (cmdPtr->flags & TCL_TRACE_DELETE) {
<           flags &= ~TCL_TRACE_DELETE;

Let me know if the 2nd complaint persists.

Hemang.
msoferplain2002-09-06 01:19:19
Logged In: YES 
user_id=148712

First leak plugged by the patch; second still there ...
hemanglavanaplain2002-09-06 04:15:04
Logged In: YES 
user_id=81875

Okay, here's a combined patch to fix both the memory leaks. 

Hemang.

Index: tclBasic.c
=============================================
======================
RCS file: /cvsroot/tcl/tcl/generic/tclBasic.c,v
retrieving revision 1.69
diff -r1.69 tclBasic.c
2003a2004,2010
>     /*
>      * Script for rename traces can delete the 
command "oldName".
>      * Therefore increment the reference count for cmdPtr so 
that
>      * it's Command structure is freed only towards the end 
of this
>      * function by calling TclCleanupCommand.
>      */
>     cmdPtr->refCount++;
2025a2033,2038
>     /*
>      * Now free the Command structure, if the "oldName" 
command has
>      * been deleted by invocation of rename traces.
>      */
>     TclCleanupCommand(cmdPtr);
> 
2525,2527c2538,2545
<        * While a rename trace is active, we will not process 
any more
<        * rename traces; while a delete trace is active we will 
not
<        * process any more delete traces 
---
>          * While a rename trace is active, we will not process 
any more
>          * rename traces; while if a delete trace is active we 
will never
>          * reach here -- because 
Tcl_DeleteCommandFromToken checks for the
>          * condition (cmdPtr->flags & CMD_IS_DELETED) 
and returns immediately
>          * when a command deletion is in progress. For all 
other traces,
>          * delete traces will not be invoked but a call to 
TraceCommandProc
>          * will ensure that tracePtr->clientData is freed 
whenever the
>          * command "oldName" is deleted.
2531,2533d2548
<       }
<       if (cmdPtr->flags & TCL_TRACE_DELETE) {
<           flags &= ~TCL_TRACE_DELETE;
dgpplain2002-09-06 07:12:17
Logged In: YES 
user_id=80530

Test script in report leaks before patch.
Does not leak after patch.
Test suite still passes after patch.
Committing.
Adding script in report to test suite, so
Purify testing in the future will catch
any recurrence of this problem.

Thanks, Hemang!
Log in (anonymous is fine) to add a comment.