Skip to content
Tcl Source Code View Ticket
Login

View Ticket

fCmd-4.5 leaks

Closed / Fixed3389764fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2011-08-10 19:01:33
$ valgrind --leak-check=yes --num-callers=10 ./tcltest ../tests/fCmd.test -match fCmd-4.5
...
==16368== 344 (48 direct, 296 indirect) bytes in 1 blocks are definitely lost in loss record 22 of 23
==16368==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==16368==    by 0x498624: Tcl_Alloc (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==16368==    by 0x4FB37A: TclCreateLiteral (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==16368==    by 0x4FB5B1: TclRegisterLiteral (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==16368==    by 0x4ACF31: TclCompileVariableCmd (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==16368==    by 0x4CE82F: TclCompileScript (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==16368==    by 0x4D0A3B: TclSetByteCodeFromAny (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==16368==    by 0x4D0B86: SetByteCodeFromAny (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==16368==    by 0x4479EE: TclProcCompileProc (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==16368==    by 0x447C16: PushProcCallFrame (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
dgpplain2011-08-11 02:52:58
Reduced leaking script:

package require tcltest
tcltest::test foo desc {catch {file mkdir ""}} 1
dgpplain2011-08-11 03:33:14
strangely, at least one point where the
leak was introduced is in checkin
[85f6cc0b99] (2009-07-26)
dkfplain2011-08-11 14:51:25
Whatever is going on, as that's a patch of just Tcl code, it must be exposing an issue elsewhere.
ferrieuxplain2011-08-11 17:38:12
Slowly getting to the offending object -- unfortunaltely the above stack is hit many times.
However, I think the leaked literal is "". Indeed, if we replace the constant "" in the test with a global, the leak disappears:

  set empty ""
  test blah Blah -setup {} -constraints {} -returnCodes error -body {
    file mkdir $::empty
  } -result {can't create directory "": no such file or directory}
ferrieuxplain2011-08-11 17:57:37
Continuing on this vein. Apparently the leak occurs when the "" shimmers from bytecode to path:

package require tcltest
proc bad s {
    puts [tcl::unsupported::representation $s]
    catch {file mkdir $s}
    puts [tcl::unsupported::representation $s]
}
set z ""
tcltest::test blah Blah -setup {   } -constraints {   } -returnCodes error -body {
    #bad $::z
    bad ""
} -result {can't create directory "": no such file or directory}


Leaking case (bad ""):
value is a bytecode with a refcount of 58, object pointer at 0x424bfb8, internal representation 0x43547c0:(nil), string representation "".
value is a path with a refcount of 60, object pointer at 0x424bfb8, internal representation 0x52a1910:(nil), string representation "".

Non-leaking case (bad $::z):
value is a pure string with a refcount of 4, object pointer at 0x45918d0, string representation "".
value is a path with a refcount of 6, object pointer at 0x45918d0, internal representation 0x52a0e88:(nil), string representation "".

(note the +2 on refcount here is in both cases and is a red herring, due to errorstack conservation. can be avoided by sprinkling [catch {error foo}] in the code)
ferrieuxplain2011-08-11 18:01:31
Hmm, "being a bytecode" is not sufficient, as is shown by adding "eval $::z".
So I guess the criterion is rather belonging to different literal pools.
dgpplain2011-08-11 21:51:16
I'm at a loss.  Starting again today, and the leak
is simply gone.  I do not get it.
dgpplain2011-08-11 21:57:03
never mind.  The testing recipe is too fragile
for less than full attention.
ferrieuxplain2011-08-12 01:20:08
Making progress:
- I confirm that the leaked object is that hugely shared empty string
- differential gdb-watching of its refcount (with and without the [file mkdir ""] line shows that the extra Incr is done by DupFsPathInternalRep  at tclPathObj.c:2623
Maybe this leak is masked when the "" is from the other pool, being freed wholesale ?
dgpplain2011-08-12 01:39:44
Do we somehow end up with a cycle of
paths?  Where A has B as its translated form,
and B has A as its translated form, and their
mutual dependence prevents refcounts falling to 0 ?
ferrieuxplain2011-08-12 01:51:06
Think I got it:
(1) In TclByteCodeInit, the comment:


    /*
     * Prevent circular reference where the bytecode intrep of
     * a value contains a literal which is that same value.
     * If this is allowed to happen, refcount decrements may not
     * reach zero, and memory may leak.  Bugs 467523, 3357771
     */

explains why a Dup is done on the object (The Empty String, let's call it x0, shared 60 times).

(2) At the time this Dup is done (for an obscure compilation of an empty body), the object has just shimmered to a Path (because of file mkdir), hence DupFsPathInternalRep is called.

(3) However, x0 satisfies: x0->intrep->translatedPathPtr == x0
(4) Since DupFsPathInternalRep copies the translatedPathPtr, the result of the Dup is an object x1 such that
 x1->intrep->translatedPathPtr == x0

(5) As a consequence, the very thing that (1) was supposed to avoid, a reference loop, happens: the Dup has been too lazy.
dgpplain2011-08-12 01:57:31
Seems that a less wretched plan (nothing about the "path"
type can be considered 'better' :) ) would be for the Dup
of  path containing an internal cycle to produce a new
path that also contains an internal cycle, rather than
containing links to the value just Dup'd .
dgpplain2011-08-12 01:58:32
You want to try that, or shall I, or do you
prefer something completely different?
dgpplain2011-08-12 03:07:51
Branch bug-3389764 has the proposed fix.
With that patch in place, I see all of fCmd.test
as leak free.
ferrieuxplain2011-08-12 05:46:48
Looks perfect to me :)
dgpplain2011-08-12 23:12:46
allow_comments - 1
Log in (anonymous is fine) to add a comment.