C++ Starter: Memory leaks

C++ Adventures is a section where I mostly speak of my encounters and struggle while learning the C++ language. This section by no means may contain insights of C++ as a language, or any factual content that may be used as a point of discussion. I’m just a penguin on a journey.

Yesterday I spent some time hunting a memory leak that was caused in a separate thread from the main GUI one. I noticed the memory first almost by accident I had the system monitor opened and with a quick glance I noticed my application had sucked 1.4GiB of ram by letting it poll through over 64 sites per second. That caused a collection of unmanaged QNetworkReply to pile up.

The way I got around to fix it I believe isn’t the “correct way” but it works for me. Hopefully someday someone will tell me what I was doing wrong, sadly I don’t know anyone I could befriend and chit-chat about my encounters.

[code lang=”cpp”]

void SomeClass::requestFinished(QNetworkReply* reply) {

// process data
reply->setParent(0); //isolate from parent
reply->deleteLater(); // let Qt handle the rest

Leaving your network replies is bad. Even Qt documentation states that the developer has to deal with each reply disposal. It can get really ugly fast if you do constant polls (2-3 seconds) with QNetworkAccessManager and leave things unmanaged.

Now, another subject I wanted to talk about is memory usage and the root of all evil that is premature optimization. Don’t worry, I haven’t fell into a pit of despair after running into my first memory leak.

Lately, I’ve been pondering that on the GNOME 3 environment my application, stripped out of networking stuff I’m working on consumes 18MiB memory, but once I add the networking side it bumps to 24MiB. Where am I going with this exactly? Well, I’ve been pondering on how a person can exercise mindful techniques to not let your application use something more than 80MiB+.

Here’s the thing though, 20, 60, 80 MiB is nothing these days. For a feed reader it might look awkward. At first I felt like 25MiB seemed too much and I’m not even done with my application. Worrying about it is not something I’m constantly doing though.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.