简体   繁体   中英

C++/Qt memory Leak?

I noticed a very interesting finding. I was testing my application involving a custom made GUI element which was being updated by data coming from an external source. The update of GUI was done using a slot(Qt specific detail) function whenever data arrived on serial port. Now the data was coming a rate of 10 packets a second ie the update GUI function was being called 10 packets a second. This had the effect of slowing down the application in addition to constantly increasing its memory footprint. It started at 60 MB and increased to 65 MB in a couple of hours.

My conclusion was that updating the GUI is slow and when a slot for updating is called 10 times a second, the slot calls are queued in the long run thus degrading application response time.

I solved this problem by caching the incoming value and updating the GUI on when there is change in the incoming value.

I have tried various free tools like valgrind-memcheck, leak checker but there results aren't helpful, in fact leak checker doesn't find the leak but my programs memory size is increasing constantly. Does that mean it is because of queuing of signal slot connection as GUI update is inherently slow?

Now here lies the issue. Tracking down memory leaks is hard enough and if Qt is involved how can a hapless programmer be sure of the problem ie if its actually a memory leak or queuing of signal slot connections?

The memory may be fragmented. AFAIK memory is only given back to the OS iff it is page aligned and a multiple of a page size. Some memory allocators don't ever give memory back, googles malloc comes to mind.

So if Qt or your app constantly mallocs and frees small blocks of randomly different sizes, the memory may be kept. The increase of the memory footprint should slow down over time though.

If you are updating some QWidget like object, make sure to use update() and not repaint().

Does that mean it is because of queuing of signal slot connection as GUI update is inherently slow?

Yes ...

Stop the incomming events and your memory must be released after a while.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM