Qt ------ 内存回收机制、new对象的回收

写在前面的总结:

建议:对于不能指定父对象的对象(对象通过moveToThread()移入其他线程、没有继承QObject的类产生的对象),在其他线程通过deleteLater()内存回收,其他通过指定父对象进行内存回收

 

对于Qt的内存回收机制

1、手动删除

  1.1 对于new出来的对象,如果对象调用deleteLater()而不是使用delete,只有当控制器返回事件循环,才会析构对象,deleteLater() 函数可以用于析构不在本线程中的对象

  1.2 对于new出来的对象,如果直接 delete 对象,内存能立马被回收

2、Qt半自动回收

  Qobject的子类,如果创建对象的时候指定父对象,当父对象被delete,会先把子对象销毁

 

 

 

 

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.

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值