Solved What is the difference between delete and deleteLater
-
I wonder the difference between delete and deleteLater.
I try to use these in my code, but My app was call SIGSEGV when I used [delete].It code:
void CcuTcp::fnReconnectTcp(QString sTcpConnectIP, quint16 uiTcpConnectPort, QString sTcpBindIP) { if( m_pTcpSocket != nullptr ) { m_pTcpSocket->close(); //delete m_pTcpSocket; //call SIGSEGV m_pTcpWifiSocket->deleteLater(); //no call SIGSEGV } m_pTcpSocket = new QTcpSocket(); fnTcpClientSet( sTcpConnectIP, uiTcpConnectPort, sTcpBindIP); }
This code is a function for reconnecting the Tcp client.
It work automatically if the server is not found or is lost.If I use [delete], After a few seconds, SIGSEGV will be called.
But if I use [deleteLate],SIGSEGV will not be called.What is the difference between the two?
Also, is it better to use deleteLater in other situations as well?(For example, in Destructor) -
Hi,
delete
is the standard C++ deletion instruction that will nuke the object right now.deleteLater
schedules the object deletion trough the event loop. See its documentation for more details. -
@w-tkm said in What is the difference between delete and deleteLater:
I wonder the difference between delete and deleteLater.
I try to use these in my code, but My app was call SIGSEGV when I used [delete].I think you could find all information in documentation => https://doc.qt.io/qt-5/qobject.html#deleteLater.
In short: with
delateLater()
the object will be destroyed on next event loop iteration, this will ensure all attached signals/slots for this instance will be nicely handled.[EDIT] @SGaist was faster ;)
-
Oh, I know! thanks!
When I used delete, some signals/slots were not nicely handled.Thank you, both of you.