How to find memory leaks in Qt Creator with the help of Deleaker
This tutorial explains how to detect memory leaks in C++ applications developed with Qt Creator using Deleaker.
Installation
Deleaker can be used either as a standalone application or as a Qt Creator plugin. Deleaker Standalone is useful, for example, when Qt Creator is not available on the target machine.
When used as a plugin, Deleaker allows developers to find memory leaks without leaving Qt Creator, making it easier to navigate directly to the source of potential issues.
After installation, a new Deleaker menu item is added to the Qt Creator main menu, or under Tools in Qt Creator 17 and later. To open the Deleaker window, simply select it:
For Qt Creator 17 and later:
Adding a memory leak
Create a new project (Qt Widgets Application) to see how Deleaker detects memory leaks.
Introduce a leak by allocating an instance of QPushButton that is not added to any layout or parent widget. This simulates a common mistake where an object is allocated but never properly managed:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget* widget = new QWidget();
QVBoxLayout* layout = new QVBoxLayout();
QPushButton* button = new QPushButton("button");
QPushButton* button1 = new QPushButton("button1");
layout->addWidget(button);
widget->setLayout(layout);
setCentralWidget(widget);
}Taking snapshots
Start debugging the application. When the main window is displayed, switch back to Qt Creator and click Take Snapshot to inspect all currently allocated resources, including memory allocations:
Final snapshot
When the process exits, Deleaker automatically takes a final snapshot containing all allocations that were not freed. Such allocations are considered memory leaks.
For each memory block, you can inspect the call stack to determine where the allocation was made.
If source file and line number information is available, Deleaker opens the source file directly in the code editor when you double-click an entry or use the context menu:
How to fix memory leakage
In some cases, a process continuously allocates memory without releasing it. Depending on the application logic, this behavior may be expected or may indicate a bug.
To demonstrate this scenario, add a timer that allocates memory every 50 milliseconds:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(onTimer()));
timer->start(50);
}
void MainWindow::onTimer()
{
int* p = new int[10000];
}Monitoring resource usage
The easiest way to observe increasing memory usage is to open the resource usage graph in the Deleaker window:
All allocations are grouped by call stack. The Hit Count column shows how many allocations share the same call stack, meaning they were allocated at the same location in the code.
Sorting by Hit Count quickly highlights code locations that continuously allocate resources:
Comparing consecutive snapshots
Another effective strategy is to compare consecutive snapshots to identify newly allocated resources.
First, take a base snapshot. Then allow the process to run for some time so that more memory is allocated, and take a second snapshot. Use Compare with... to compare the two snapshots and view only allocations that were created after the base snapshot.
If the result is empty, clear all filters to display all allocations without exception: