当前位置:网站首页>A problem and solution of recording QT memory leakage

A problem and solution of recording QT memory leakage

2022-07-05 05:25:00 Passing bears~

brief introduction

The phenomenon :
qt Sometimes used in new Not used after delete
reason :
Qt Automatic recycling depends on the relationship between father and son . Father destroyed it . His children were also destroyed .

Example

#include "mainwindow.h"
#include <QApplication>
#include <QTextCodec>
#include <QLabel>

int main(int argc, char *argv[])
{
    
    QApplication a(argc, argv);
    MainWindow w;
    QLabel *label =new QLabel("hello",&w);
    // Use here new There is no need to perform delete, because label The parent class is w, and w Is to create... In the stack , It will be released automatically when the program is closed , So as w Subclass memory is also released .

    QLabel *label1 =new QLabel("world");
    // This needs to be executed delete label1, Otherwise, memory leakage will occur , because label No parent , So not for label Free memory .
    w.show();
    a.exec();
    delete label1;
    label1=NULL;
    return 0;
}
原网站

版权声明
本文为[Passing bears~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050517374161.html