| Ticket UUID: | 28a3c366e628495ecb63c2cbf4aad582b47c0f29 | |||
| Title: | Memory leak in Tk text widgets | |||
| Type: | Bug | Version: | 8.6.6 | |
| Submitter: | anonymous | Created on: | 2017-03-21 19:57:52 | |
| Subsystem: | 18. [text] | Assigned To: | fvogel | |
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Closed | Last Modified: | 2017-03-29 20:04:53 | |
| Resolution: | Fixed | Closed By: | fvogel | |
| Closed on: | 2017-03-29 20:04:53 | |||
| Description: |
This test script leaks memory, the more loops that take place the more it leaks.
package require Tk
text .text -undo false
pack .text
for { set lines 0 } { $lines < 100000 } { incr lines } {
.text insert end "padding $lines\n"
if { $lines > 1000 } {
.text replace 1.0 3.0 "replace $lines\n"
}
}
destroy .
exit
Running valgrind on it on Linux gives (partial list): $ valgrind --num-callers=16 --leak-check=full --show-leak-kinds=all -v tclsh8.6 test.tcl ... ==31624== 245,760 bytes in 15 blocks are definitely lost in loss record 281 of 281 ==31624== at 0x482E27C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==31624== by 0x49851D6: GetBlocks (tclThreadAlloc.c:1044) ==31624== by 0x49842EE: TclpAlloc (tclThreadAlloc.c:358) ==31624== by 0x4876927: Tcl_Alloc (tclCkalloc.c:1059) ==31624== by 0x5312BEB: TkTextIsElided (tkTextBTree.c:3502) ==31624== by 0x531C5A7: IsStartOfNotMergedLine (tkTextDisp.c:6911) ==31624== by 0x5319AC3: TextChanged (tkTextDisp.c:4793) ==31624== by 0x53199E9: TkTextChanged (tkTextDisp.c:4736) ==31624== by 0x530A201: InsertChars (tkText.c:2683) ==31624== by 0x530B7E7: TextInsertCmd (tkText.c:3691) ==31624== by 0x5308065: TextWidgetObjCmd (tkText.c:1390) ==31624== by 0x486B722: Dispatch (tclBasic.c:4357) ==31624== by 0x486B7A1: TclNRRunCallbacks (tclBasic.c:4390) ==31624== by 0x486B0C1: Tcl_EvalObjv (tclBasic.c:4120) ==31624== by 0x486D0D4: TclEvalEx (tclBasic.c:5259) ==31624== by 0x495149D: Tcl_FSEvalFileEx (tclIOUtil.c:1826) ... The leak is small (2 bytes per loop) but persistent, more loops leak more. In a data logging application the leaked memory could easily reach many megabytes. There appears to me to be no way that the memory can actually be leaking in TkTextIsElided since every ckalloc has a matching ckfree. Compiling with "-DTCL_MEM_DEBUG=1" confirms this. The only conclusion I can make is that the memory leak is in the memory allocator and not in the TkTextIsElided function itself. This patch fixes it by removing a malloc and free in each call to the function by having a one-time allocated structure that can be re-used on each call (never returned to the caller).
--- tk8.6.6/generic/tkTextBTree.c 2017-03-21 19:19:05.046985939 +0000
+++ tk8.6.6/generic/tkTextBTree.new.c 2017-03-21 19:04:37.055536518 +0000
@@ -3497,9 +3497,12 @@
register TkTextElideInfo *infoPtr;
TkTextLine *linePtr;
int elide;
+ static TkTextElideInfo *localInfoPtr=NULL;
if (elideInfo == NULL) {
- infoPtr = ckalloc(sizeof(TkTextElideInfo));
+ if (localInfoPtr == NULL)
+ localInfoPtr = ckalloc(sizeof(TkTextElideInfo));
+ infoPtr=localInfoPtr;
} else {
infoPtr = elideInfo;
}
@@ -3633,8 +3636,6 @@
ckfree(infoPtr->tagCnts);
ckfree(infoPtr->tagPtrs);
}
-
- ckfree(infoPtr);
}
return elide;
| |||
| User Comments: |
fvogel added on 2017-03-29 20:04:53:
Thanks for the report. This fix is now merged into core-8-6-branch and trunk. Closing now. Many thanks to the anonymous "Original bug reporter" who clearly reported, and who provided patches. I wished I could have given credits to you in the commit log. dgp added on 2017-03-29 19:30:33:
I confirm that when the code at the tip
of core-8-6-7-rc branch is used, the
demo script in this ticket shows a
memory leak in valgrind:
definitely lost: 80,648 bytes in 19,689 blocks
When I test the branch bug-28a3c366e6, things are
much better:
definitely lost: 576 bytes in 5 blocks
So I think a real leak has been found and fixed
and the patch should be merged into 8.6 and
forward. (I'll pull to 8.6.7 from there)
Sadly, Tk has never been given an aggressive cleanup
to purge leaks and memory faults, so it's not currently
possible to strive for "definitely lost: 0 bytes" like
we do for Tcl on a regular basis.
Thanks for the work reporting and fixing!
fvogel added on 2017-03-27 20:46:35: What dgp (Don Porter) wanted to say is that you have to compile not only with -DTCL_MEM_DEBUG=1 but you should also give the -DPURIFY flag, otherwise the results cannot be trusted.
There are other tricks to know as well, perhaps reading the following wiki page could be useful:
http://wiki.tcl.tk/28634
anonymous (claiming to be Original bug reporter) added on 2017-03-27 18:00:08: Running valgrind with the patch shows the following - the key difference is that it says "possibly lost" rather than "definitely lost". ==31562== 98,304 bytes in 6 blocks are possibly lost in loss record 279 of 280 ==31562== at 0x482E27C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==31562== by 0x49851D6: GetBlocks (tclThreadAlloc.c:1044) ==31562== by 0x49842EE: TclpAlloc (tclThreadAlloc.c:358) ==31562== by 0x4876927: Tcl_Alloc (tclCkalloc.c:1059) ==31562== by 0x5312C38: TkTextIsElided (tkTextBTree.c:3503) ==31562== by 0x531C634: IsStartOfNotMergedLine (tkTextDisp.c:6911) ==31562== by 0x5319B50: TextChanged (tkTextDisp.c:4793) ==31562== by 0x5319A76: TkTextChanged (tkTextDisp.c:4736) ==31562== by 0x530A231: InsertChars (tkText.c:2683) ==31562== by 0x530B817: TextInsertCmd (tkText.c:3691) ==31562== by 0x5308095: TextWidgetObjCmd (tkText.c:1390) ==31562== by 0x486B722: Dispatch (tclBasic.c:4357) ==31562== by 0x486B7A1: TclNRRunCallbacks (tclBasic.c:4390) ==31562== by 0x486B0C1: Tcl_EvalObjv (tclBasic.c:4120) ==31562== by 0x486D0D4: TclEvalEx (tclBasic.c:5259) ==31562== by 0x495149D: Tcl_FSEvalFileEx (tclIOUtil.c:1826) My theory for what is happening is that TkTextIsElided calls ckalloc which eventually calls malloc, then TkTextIsElided calls ckfree which doesn't call free on the memory. When the tkTextBTree.c functions need some memory they call ckalloc which gives them space in the block that TkTextIsElided was using and stops it being freed. The TkTextIsElided function allocates and then frees 8 kB each time it is called and the tkTextBTree.c function is only losing 2 Bytes every time it rebalances the B-tree. It is far more likely that ckalloc needs to call malloc for the TkTextIsElided function than for the B-tree so that is the one that valgrind reports. If this is right it explains what 'dgp' said about valgrind producing invalid results about the source of the leak. Just for reference, the way that I found the real bug was to compile a memory debug version "-DTCL_MEM_DEBUG=1" and adding this line to the script: memory active "memory.log" Then by running for a small number of iterations compared to a large number of iterations the difference in the dumped "memory.log" files told me where the memory was being being allocated and from that it was easy to find the reason. fvogel added on 2017-03-26 09:11:44: This makes much more sense to me indeed. I have committed your patch in branch bug-28a3c366e6. Your test script does no longer show memory increase while running (to have the time to observe this in the Win task manager I had to increase the stop bound of the loop to 10x). Analyzing the code it seems your findings are correct: freeing numPixels seems to be missing at the places you pointed your finger to. This was the case since the beginning of Tk history. One place in tkTextBTree.c:879 received the necessary free when TIP #169 was implemented, I guess the other places were forgotten. Finally, on Vista running the test suite for the text widget (i.e. with TESTFLAGS="-file text*.test") shows the same results for me as before the patch: zero failure, and of course no crash. Last thing you could perhaps please check: what does valgrind spit now with your fix? Is it happier now? anonymous (claiming to be Original bug reporter) added on 2017-03-24 19:23:08: I have had another look and found that it really is a memory leak in the text widget and not in the memory handling subsystem. In some places where a BTree node is freed the numPixel element is also freed, but in some case it is not. This patch fixes all places that I think the error exists - it may not be complete; please check. --- tk8.6.6/generic/tkTextBTree.orig.c 2017-03-21 19:19:05.046985939 +0000 +++ tk8.6.6/generic/tkTextBTree.c 2017-03-24 19:11:00.718683711 +0000 @@ -1439,6 +1439,7 @@ prevNodePtr->nextPtr = curNodePtr->nextPtr; } parentPtr->numChildren--; + ckfree(curNodePtr->numPixels); ckfree(curNodePtr); curNodePtr = parentPtr; } @@ -4185,6 +4186,7 @@ treePtr->rootPtr = nodePtr->children.nodePtr; treePtr->rootPtr->parentPtr = NULL; DeleteSummaries(nodePtr->summaryPtr); + ckfree(nodePtr->numPixels); ckfree(nodePtr); } return; @@ -4274,6 +4276,7 @@ nodePtr->nextPtr = otherPtr->nextPtr; nodePtr->parentPtr->numChildren--; DeleteSummaries(otherPtr->summaryPtr); + ckfree(otherPtr->numPixels); ckfree(otherPtr); continue; } Here is the test case execution with no increase in memory usage in a minute compared to around 10 MB in the same time before. $ while true; do sleep 10 ; ps aux | grep tclsh | grep -v grep ; done anonymous 22117 99.2 0.1 15132 11436 ? Rs 19:07 0:28 tclsh8.6 test.tcl anonymous 22117 99.4 0.1 15132 11436 ? Rs 19:07 0:38 tclsh8.6 test.tcl anonymous 22117 99.6 0.1 15132 11436 ? Rs 19:07 0:48 tclsh8.6 test.tcl anonymous 22117 99.6 0.1 15132 11436 ? Rs 19:07 0:58 tclsh8.6 test.tcl anonymous 22117 99.7 0.1 15132 11436 ? Rs 19:07 1:08 tclsh8.6 test.tcl anonymous 22117 99.8 0.1 15132 11436 ? Rs 19:07 1:18 tclsh8.6 test.tcl anonymous 22117 99.8 0.1 15132 11436 ? Rs 19:07 1:28 tclsh8.6 test.tcl anonymous 22117 99.8 0.1 15132 11436 ? Rs 19:07 1:38 tclsh8.6 test.tcl anonymous (claiming to be Orignal bug reporter) added on 2017-03-22 18:46:18: The increased memory usage is also seen on Linux using 'ps'; it is not just an artifact of valgrind. Below is the memory use increasing when the number of iterations in the test case is increased (the amount of text in the text widget is not increasing, the memory increase is only from the leak). $ while true; do sleep 10 ; ps aux | grep tclsh | grep -v grep ; done anonymous 5903 98.7 0.2 16372 12580 ? Rs 18:39 0:06 tclsh8.6 test.tcl anonymous 5903 99.5 0.2 18820 15220 ? Rs 18:39 0:16 tclsh8.6 test.tcl anonymous 5903 99.7 0.2 21400 17596 ? Rs 18:39 0:26 tclsh8.6 test.tcl anonymous 5903 99.8 0.3 23848 20236 ? Rs 18:39 0:36 tclsh8.6 test.tcl anonymous 5903 99.9 0.3 26440 22612 ? Rs 18:39 0:46 tclsh8.6 test.tcl anonymous 5903 99.9 0.4 28876 25252 ? Rs 18:39 0:56 tclsh8.6 test.tcl I agree that the patch is just covering up the root cause. The 'TkTextIsElided' function has a comment in it that says "Almost always avoid malloc, so stay out of system calls.". This is mostly true, but nearly every call to the function will have one malloc. You can consider the patch as an optimisation for the function to avoid all mallocs in most cases rather than a fix for the root cause. dgp added on 2017-03-22 15:17:24: Please be aware that valgrind testing on Tcl needs a -DPURIFY build to produce useful results. fvogel added on 2017-03-21 21:41:24: Since from your analysis the issue is not in the text widget but in the memory allocator, I don't really understand why you are proposing to change the text widget. Your proposal is a workaround, but it will hide the real problem under the carpet, don't you think so? | |||
Home
Timeline
Branches
Tags
Forum
Tickets
Download
Wiki