关于QT中deleteLater是否会造成内存泄漏的疑惑

luike 2018-05-30 08:35:24
一个继承与QThread,重写run的类,如
class CMyThread : public QThread
{
private:
void run();
}
而run的实现是这样
void CMyThread ::run()
{
QTimer timer;//定时器,做超时检测
QEventLoop loop;
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
m_pLabel = new MyLabel;
timer.start(100);
loop.exec();
m_pLabel->deleteLater();
}
其中m_pLabel 是一个派生于QObject的类对象。
CMyThread 类对象在主UI线程中创建并start,那么这里m_pLabel是否能够被析构?是否存在内存泄漏?
我的理解是,定时器时间到,loop执行了quit之后,当前线程中就没有消息循环了,deleteLater发送的删除消息没有消息循环接收,也就没机会执行。不知道理解的正确不正确?
...全文
1919 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
luike 2018-05-30
  • 打赏
  • 举报
回复
引用 4 楼 goldenhawking 的回复:
[slot] void QObject::deleteLater() Schedules this object for deletion. The object will be deleted when control returns to the event loop. If the event loop is not running when this function is called (e.g. deleteLater() is called on an object before QCoreApplication::exec()), the object will be deleted once the event loop is started. If deleteLater() is called after the main event loop has stopped, the object will not be deleted. Since Qt 4.8, if deleteLater() is called on an object that lives in a thread with no running event loop, the object will be destroyed when the thread finishes. Note that entering and leaving a new event loop (e.g., by opening a modal dialog) will not perform the deferred deletion; for the object to be deleted, the control must return to the event loop from which deleteLater() was called. Note: It is safe to call this function more than once; when the first deferred deletion event is delivered, any pending events for the object are removed from the event queue.
好棒! 解释了!我看的是比较古老的版本。。。。 Copied from the doc: void QObject::deleteLater () [slot] Schedules this object for deletion. The object will be deleted when control returns to the event loop. If the event loop is not running when this function is called (e.g. deleteLater() is called on an object before QCoreApplication::exec()), the object will be deleted once the event loop is started. Note that entering and leaving a new event loop (e.g., by opening a modal dialog) will not perform the deferred deletion; for the object to be deleted, the control must return to the event loop from which deleteLater() was called. Note: It is safe to call this function more than once; when the first deferred deletion event is delivered, any pending events for the object are removed from the event queue. See also destroyed() and QPointer.
  • 打赏
  • 举报
回复
文档里写的很清楚哦!线程内的: . Since Qt 4.8, if deleteLater() is called on an object that lives in a thread with no running event loop, the object will be destroyed when the thread finishes. Qt 设计的太棒了!哈哈!
  • 打赏
  • 举报
回复
[slot] void QObject::deleteLater() Schedules this object for deletion. The object will be deleted when control returns to the event loop. If the event loop is not running when this function is called (e.g. deleteLater() is called on an object before QCoreApplication::exec()), the object will be deleted once the event loop is started. If deleteLater() is called after the main event loop has stopped, the object will not be deleted. Since Qt 4.8, if deleteLater() is called on an object that lives in a thread with no running event loop, the object will be destroyed when the thread finishes. Note that entering and leaving a new event loop (e.g., by opening a modal dialog) will not perform the deferred deletion; for the object to be deleted, the control must return to the event loop from which deleteLater() was called. Note: It is safe to call this function more than once; when the first deferred deletion event is delivered, any pending events for the object are removed from the event queue.
  • 打赏
  • 举报
回复
实验结果:会的。

#ifndef TESTOBJ_H
#define TESTOBJ_H
#include <QObject>
class TestObj : public QObject
{
	Q_OBJECT
public:
	explicit TestObj(QObject *parent = nullptr);
	~TestObj();
};
#endif // TESTOBJ_H

#include "testobj.h"
#include <QDebug>
TestObj::TestObj(QObject *parent) : QObject(parent)
{
	qDebug()<<"构造函数";
}
TestObj::~TestObj()
{
	qDebug()<<"析构函数";
}

run函数:

void QMyThread::run()
{
	QTimer timer;//定时器,做超时检测
	QEventLoop loop;
	connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
	m_pObj = new TestObj;
	timer.start(1000);
	loop.exec();
	m_pObj->deleteLater();
}

输出:

Starting D:\FTP\build-untitled-mingw32-Debug\debug\untitled.exe...
构造函数
析构函数
D:/FTP/build-untitled-mingw32-Debug/debug/untitled.exe exited with code 0
luike 2018-05-30
  • 打赏
  • 举报
回复
引用 1 楼 goldenhawking 的回复:
建议动手实验哦! 从 QObject派生一个TestObj,而后在析构函数中 qDebug()<<"Destructure!"; 试试看有没有输出?
试过了,析构函数确实被调用了,但是还是有点不太明白。为什么?
  • 打赏
  • 举报
回复
建议动手实验哦! 从 QObject派生一个TestObj,而后在析构函数中 qDebug()<<"Destructure!"; 试试看有没有输出?

65,189

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧