当前位置:网站首页>记录QT内存泄漏的一种问题和解决方案
记录QT内存泄漏的一种问题和解决方案
2022-07-05 05:17:00 【路过的小熊~】
简介
现象:
qt中有时候使用new后并没有使用delete
原因:
Qt 自动回收是靠父子关系。父亲销毁了。他的孩子也销毁。
示例
#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);
//这里使用new之后不需要执行delete,因为label的父类是w,而w是在栈中创建,在程序关闭的时候会自动释放,所以作为w的子类内存也被释放。
QLabel *label1 =new QLabel("world");
//这个是需要执行delete label1,否则会造成内存泄漏,因为label没有父类,所以不会为label释放内存.
w.show();
a.exec();
delete label1;
label1=NULL;
return 0;
}
边栏推荐
- 64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
- [interval problem] 435 Non overlapping interval
- UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
- [LeetCode] 整数反转【7】
- National teacher qualification examination in the first half of 2022
- Embedded database development programming (VI) -- C API
- [allocation problem] 455 Distribute cookies
- Under the national teacher qualification certificate in the first half of 2022
- Haut OJ 1221: a tired day
- Quick sort summary
猜你喜欢
[转]MySQL操作实战(三):表联结
嵌入式数据库开发编程(零)
[turn to] MySQL operation practice (I): Keywords & functions
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
Shell Sort
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
Unity ugui source code graphic
2022/7/2做题总结
TF-A中的工具介绍
Web APIs DOM节点
随机推荐
嵌入式数据库开发编程(零)
room数据库的使用
Unity check whether the two objects have obstacles by ray
A complete attack chain
[allocation problem] 135 Distribute candy
Haut OJ 1357: lunch question (I) -- high precision multiplication
[binary search] 69 Square root of X
Haut OJ 1243: simple mathematical problems
Cocos create Jiugongge pictures
2022年上半年国家教师资格证考试
被舆论盯上的蔚来,何时再次“起高楼”?
Shell Sort
Bucket sort
C # perspective following
Reverse one-way linked list of interview questions
Haut OJ 1245: large factorial of CDs --- high precision factorial
远程升级怕截胡?详解FOTA安全升级
使用命令符关闭笔记本自带键盘命令
cocos2dx_ Lua card flip
[转]MySQL操作实战(一):关键字 & 函数