写在前面的总结:
建议:对于不能指定父对象的对象(对象通过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.