小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Using valgrind with Qt Creator

 Harrison.Ding 2012-07-18

This article will show you how you can use valgrind on Linux to find memory leaks in your Qt based application from within Qt Creator.

valgrind is a low level tool that can do many useful memory-related things, of which we will focus on locating memory leaks - especially useful for long running processes and those that create and destroy a lot of objects. To do this, valgrind tracks the memory allocation/deallocations your application does and generates reports about places that might be losing memory.

Install valgrind

Almost all Linux distributions include valgrind, but usually do not install it by default. Just select valgrind from your package manager (shown in Kubuntu below) to install it.

Valgrind2.png

If you prefer to use the console, all you need to do on a deb based Linux is

sudo apt-get install valgrind

About 50 MB later, you will have valgrind installed


Install the (Nokia) Qt SDK or Qt Creator

If you do not already have it, you can install the Nokia Qt SDK, but the procedure below will work with any install of Qt Creator. The approach outlined in this article can be used for desktop, Maemo and Simulator targets.

Open project

Create or open an existing project. Here an example of simple application is created. Notice how this example intentionally leaks a QLabel object every time the button is pressed.

Valgrind4.png

Valgrind5.png


Create run target

In order to use valgrind, a special run target needs to be added to the project. Click the projects icon and then the 'run' section of the target you wish to debug. You will see something like

Valgrind7.png

Now click 'add' and specify a new target that looks like the following

Valgrind8.png

The complex argument line in text form for copy-pasting:

-q --tool=memcheck --leak-check=full --leak-resolution=low --suppressions=Qt47supp.txt ./[your-app-target-name]

As an initial default Qt4.7 suppression file (see below) for simulator targets you can use File:Qt47supp.txt. Of course, depending on what modules or additional libraries use, you will want to extend it.

Running the application

You will see that now in your target window you have a new run configuration selectable

Valgrind10.png

Make sure you're adding/selecting valgrind in the debug build, as it will not be able to find symbols and code lines in a release build. Upon running the application, there will be a lot of output from valgrind, intertwined with the actual output from your application. Notice that valgrind will print most of its finding when you exit the application. If you followed the article, you'll see a large amount of valgrind output.

Valgrind9.png

Interpreting the results

You will soon realize that valgrind's output can easily become overwhelming. One of the keys of dealing with valgrind efficiently is using good suppression files. Using these files avoids warnings that are false alarms or located in code outside of ours. Generally, you should pay no attention to valgrind complaints about errors in glibc or Qt - it is a common mistake to start valgrind without suppression files and then think that everything is leaking, but that is generally just the side-effect of lower level libraries deallocating memory after valgrind. Thus, to be able to focus on actual leaks in our code, a suppression file needs to be generated that will bring down the valgrind output to a manageable size containing only errors in the code at hand.

As an initial default Qt4.7 suppression file for simulator targets you can use File:Qt47supp.txt. Of course, depending on what modules or additional libraries use, you will want to extend it.

Now, save the suppression file in the build directory and re-run the application. Now you can easily spot the memory leak report with a precise source file and location where the action that caused the leak occurred.

Valgrind11.png

Qt making your life easier

An important note is that in many cases you can prevent memory leaks just by using Qt's smart pointer classesQScopedPointer and QSharedPointer being of particular interest for most common use-cases.

The QObject class also has mechanisms that help avoid dangling pointers and memory leaks. When a QObject is deleted, it emits a destroyed() signal and also automatically calls the destructor's of its child objects.

Summary

The above should start you down path of finding leaks in your Qt applications. Should you require more detailed reports or want to find out more about what valgrind can do for you, check the Valgrind manual.

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多