Bug 238948 - ktorrent plasmoid memory leak
Summary: ktorrent plasmoid memory leak
Status: RESOLVED FIXED
Alias: None
Product: ktorrent
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: Ubuntu Linux
: NOR normal
Target Milestone: ---
Assignee: Joris Guisson
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-27 00:04 UTC by Amichai Rothman
Modified: 2010-05-28 19:00 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Amichai Rothman 2010-05-27 00:04:15 UTC
Version:           unspecified (using KDE 4.4.2) 
OS:                Linux

The ktorrent plasmoid has a pretty big memory leak which reaches several MB in a few minutes (or hundreds of MB overnight). I caught this when adding the plasmoid to the desktop while ktorrent was active (with at least one active torrent), and seeing plasma-desktop grow to hundreds of MB overnight (and never go back down), and later recreating this scenario.

Reproducible: Always

Steps to Reproduce:
1. Run KTorrent with at least one active torrent (download or seed).
2. Run "valgrind --tool=memcheck --leak-check=full plasmoidviewer ktorrent"
3. Let the plasmoid run for a few minutes.
4. Close the plasmoid.
5. Observe the valgrind results.

Actual Results:  
There are several reported leaks, but the largest one is of several MB (and continues to grow with time).

Expected Results:  
No memory leaks!

Looking at the valgrind results for the largest leak:

==26361== 4,680,423 (29,664 direct, 4,650,759 indirect) bytes in 927 blocks are definitely lost in loss record 1,024 of 1,024
==26361==    at 0x4C28CC1: operator new(unsigned long) (vg_replace_malloc.c:261)
==26361==    by 0x222D663C: bt::BDecoder::parseDict() (in /usr/lib/libbtcore.so.12.3.0)
==26361==    by 0x222D6484: bt::BDecoder::decode() (in /usr/lib/libbtcore.so.12.3.0)
==26361==    by 0x240E338B: ??? (in /usr/lib/kde4/plasma_engine_ktorrent.so)
==26361==    by 0x240DBD8B: ??? (in /usr/lib/kde4/plasma_engine_ktorrent.so)
==26361==    by 0x5576F05: Plasma::DataEnginePrivate::internalUpdateSource(Plasma::DataContainer*) (dataengine.cpp:567)
...

Looking at the code it seems like torrentdbusinterface.cpp calls BDecoder::decode(), but never deletes the returned dictionary's (or other return type if exception is thrown), thus leaking on every update.

disclaimer: I don't have all the debug info available, and it's the first time I've used valgrind, and I know nothing of the libktorrent memory management conventions, so I hope I understood this correctly.

In any case, whether the analysis is correct or not - the plasmoid is definitely leaking, possibly in multiple places.
Comment 1 Joris Guisson 2010-05-27 18:14:53 UTC
SVN commit 1131207 by guisson:

Fix memory leak in plasma dataengine 

BUG: 238948

 M  +1 -0      ChangeLog  
 M  +2 -0      plasma/dataengine/torrentdbusinterface.cpp  


WebSVN link: http://websvn.kde.org/?view=rev&revision=1131207
Comment 2 Amichai Rothman 2010-05-27 22:22:22 UTC
Thanks for the quick response!

I don't know if this happens in practice, but the decode() method can return NULL or throw an exception, so it might be necessary to add a NULL check before the delete, as well as before the first time the return value is dereferenced (the line following decode()).
Comment 3 Amichai Rothman 2010-05-27 22:26:59 UTC
Scratch that first option - delete NULL is guaranteed to do nothing. The dereference might be a problem though (but is unrelated to the memory leak).
Comment 4 Joris Guisson 2010-05-28 19:00:05 UTC
Yeah I need to add a check for that.