Qt memory management and the method of locating the location of the memory leak after the leak

Qt memory management mechanism

Qt uses object parent-child relationships for memory management. When creating an object of the class, specify the parent object pointer for the object. When the parent object is destroyed and released at a certain moment, the parent object will traverse all its child objects first, and destroy and release the child objects one by one.

Qt memory management code example

QLabel *label = new QLabel;

Here is the label control that I created in the source code. No parent object is specified. You need to release the memory manually. If you don’t release it, there will be a memory leak.

QLabel *label = new QLabel();

Here is the label control that I created in the source code. It executes the constructor of QLabel without specifying the parent object. You need to release the memory manually. If you don’t release it, there will be a memory leak.

QLabel *label = new QLabel;
label->setParent(this);

The parent object is set, not only can be displayed in the parent window, but also can automatically help release memory

QStandardItemModel *itemModel = new QStandardItemModel;
ui.tableView->setModel(itemModel);

The model is only set here, and the ui control is not the parent object of the model, which cannot help the itemModel to release memory;

QStandardItem *item = new QStandardItem;
itemModel->setItem(1,item);
itemModel->clear();

Here, the model has done a clear operation, which will help release the memory occupied by the item under the model, and not release the memory of the model;

MyWidget *myWidget = new MyWidget;

The interface created by yourself also needs to release the memory space manually;

How Qt locates the location of the memory leak

Visual Leak Detector (VLD) is a free memory leak detection tool for Visual C++. The location of the memory leak can be detected using VLD. VLD supports MSVC compiler and does not support MingGW compiler. How to use VLD:
Download it from here. After downloading, you can see the include file and lib file of VLD. You don’t need to download exe from the Internet.
VLD memory leak detection programming files, including header files, library files, configuration files

Instructions

In the project file, add the corresponding include file and lib file, and the specific steps will not be explained in detail.
1. Add in the header file

#include "vld.h"

2. Place vld.ini in the same directory as the generated exe file
insert image description here

vld.ini is the configuration text of VLD. By default, the vld.ini file in the installation directory is read. When there is also a vld.ini file in the directory where the exe is located, this configuration will be read first.

Description of vld.ini parameter settings
By default, the vld.ini file in the installation directory is read. When there is also a vld.ini file in the directory where the exe is located, this configuration will be read first.

VLD:选择VLD的打开与关闭。在Debug模式下运行,关闭以后会有一行
VLD关闭的提示信息。默认为 on。

AggregateDuplicates:设置为 yes 时,相同地方产生内存泄漏只输
出一次,但是会统计发生的次数。默认是 no 。

MaxDataDump:输出的dump数据个数,默认为 256。

MaxTraceFrames:输出的调用栈的层数。默认是 64。

ReportEncoding :report 文件的编码格式,可选有 
ascii, unicode,默认是 ascii 。

ReportFile :report 文件的路径。默认是
 “.\memory_leak_report.txt”

ReportTo :可选有 debugger, file, both,debugger 
表示输出到 debug模式下的输出窗口;file 表示只输出到文件中;
 both顾名思义,全都都输出。默认是 debugger 。

Open the exe, after the operation is over, close the exe, you can see the VLD report, which contains several contents:

【Block X at …: Y bytes】:第X个块(new)泄漏了Y个字节;

【Call Stack】泄漏内存的堆栈信息,对应的行便是具体的代码位置;

【Data】泄漏内存的数据信息;

【Visual Leak Detector detected X memory leak (Y bytes).】总共 X 处泄漏,共泄漏字节数 Y 字节;

insert image description here

Some problems encountered and solutions

There is no function positioning behind the call stack (TID), for example:
insert image description here
just modify ReportEncoding = ascii in the ini file, ascii is unicode, and you can see the source code to locate the leak location after executing the program again;
insert image description here
to use VLD in release mode, Macros must be scheduled before the header file vld.h (see VLD Issues 46)

#define VLD_FORCE_ENABLE
#include "vld.h"

Guess you like

Origin blog.csdn.net/qq_43376782/article/details/130194324