当前位置:网站首页>记录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;
}
边栏推荐
- [merge array] 88 merge two ordered arrays
- Double pointer Foundation
- Chinese notes of unit particle system particle effect
- Redis has four methods for checking big keys, which are necessary for optimization
- [turn]: Apache Felix framework configuration properties
- [to be continued] [depth first search] 547 Number of provinces
- Cocos create Jiugongge pictures
- Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
- django连接数据库报错,这是什么原因
- Vs2015 secret key
猜你喜欢
Leetcode word search (backtracking method)
Double pointer Foundation
《动手学深度学习》学习笔记
小程序直播+电商,想做新零售电商就用它吧!
Merge sort
Use of snippets in vscode (code template)
YOLOv5添加注意力机制
C language Essay 1
Embedded database development programming (VI) -- C API
Redis has four methods for checking big keys, which are necessary for optimization
随机推荐
room数据库的使用
[interval problem] 435 Non overlapping interval
Unity card flipping effect
Haut OJ 1218: maximum continuous sub segment sum
Haut OJ 1347: addition of choice -- high progress addition
Lua wechat avatar URL
【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
[turn]: OSGi specification in simple terms
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
cocos_ Lua loads the file generated by bmfont fnt
Reverse one-way linked list of interview questions
Cocos create Jiugongge pictures
National teacher qualification examination in the first half of 2022
Data is stored in the form of table
[leetcode] integer inversion [7]
[binary search] 34 Find the first and last positions of elements in a sorted array
Embedded database development programming (zero)
C # perspective following
嵌入式数据库开发编程(零)
Cocos progress bar progresstimer