Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. What is the difference between delete and deleteLater
Forum Updated to NodeBB v4.3 + New Features

What is the difference between delete and deleteLater

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    w.tkm
    wrote on last edited by w.tkm
    #1

    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)

    KroMignonK 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • W w.tkm

        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)

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #3

        @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 ;)

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        0
        • W Offline
          W Offline
          w.tkm
          wrote on last edited by
          #4

          Oh, I know! thanks!
          When I used delete, some signals/slots were not nicely handled.

          Thank you, both of you.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved