Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set a pointer to an object to "nullptr" upon calling deleteLater()
Forum Update on Tuesday, May 27th 2025

How to set a pointer to an object to "nullptr" upon calling deleteLater()

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 6.0k Views 3 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.
  • A Offline
    A Offline
    alex_qwa
    wrote on last edited by
    #1

    Hi everyone,
    I have a simple application where a webSkt client connects to a webSktServer. Upon client disconnection i call the deleteLater as follows:

    connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(deleteLater());
    

    Will deleteLater() set the pWebSkt to "nullptr". I am concerned because in other parts of my code I check the validity of this pointer before doing some functions e.g.:

    if(pWebSkt)
    	pWebSkt->doJob();
    

    Another approch would be as follows:

    connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(onDisconnected());
    ....
    
    void MyClass::onDisconnected()
    {
    	delete pWebSkt;
    	pWebSkt = nullptr;
    }
    

    What would be the correct/recommended approach?

    Taz742T raven-worxR 2 Replies Last reply
    0
    • A alex_qwa

      Hi everyone,
      I have a simple application where a webSkt client connects to a webSktServer. Upon client disconnection i call the deleteLater as follows:

      connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(deleteLater());
      

      Will deleteLater() set the pWebSkt to "nullptr". I am concerned because in other parts of my code I check the validity of this pointer before doing some functions e.g.:

      if(pWebSkt)
      	pWebSkt->doJob();
      

      Another approch would be as follows:

      connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(onDisconnected());
      ....
      
      void MyClass::onDisconnected()
      {
      	delete pWebSkt;
      	pWebSkt = nullptr;
      }
      

      What would be the correct/recommended approach?

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #2

      @alex_qwa
      What do you need to delete the object if you assign nullptr?

      Do what you want.

      A 1 Reply Last reply
      0
      • A alex_qwa

        Hi everyone,
        I have a simple application where a webSkt client connects to a webSktServer. Upon client disconnection i call the deleteLater as follows:

        connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(deleteLater());
        

        Will deleteLater() set the pWebSkt to "nullptr". I am concerned because in other parts of my code I check the validity of this pointer before doing some functions e.g.:

        if(pWebSkt)
        	pWebSkt->doJob();
        

        Another approch would be as follows:

        connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(onDisconnected());
        ....
        
        void MyClass::onDisconnected()
        {
        	delete pWebSkt;
        	pWebSkt = nullptr;
        }
        

        What would be the correct/recommended approach?

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        @alex_qwa said in How to set a pointer to an object to "nullptr" upon calling deleteLater():

        What would be the correct/recommended approach?

        QPointer<QTcpSocket> pWebSkt; // gets set to NULL upon deletion
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        A 1 Reply Last reply
        5
        • Taz742T Taz742

          @alex_qwa
          What do you need to delete the object if you assign nullptr?

          A Offline
          A Offline
          alex_qwa
          wrote on last edited by
          #4

          @Taz742 , well i want to free the underlying resource (in this case the webSkt)

          mrjjM 1 Reply Last reply
          0
          • A alex_qwa

            @Taz742 , well i want to free the underlying resource (in this case the webSkt)

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @alex_qwa
            QPointer is the most elegant solution in my opinion.

            1 Reply Last reply
            0
            • raven-worxR raven-worx

              @alex_qwa said in How to set a pointer to an object to "nullptr" upon calling deleteLater():

              What would be the correct/recommended approach?

              QPointer<QTcpSocket> pWebSkt; // gets set to NULL upon deletion
              
              A Offline
              A Offline
              alex_qwa
              wrote on last edited by
              #6

              @raven-worx thanks for your prompt response;

              Your answer solves my problem, BUT ...

              1- QPointer should be used when i need a pointer to an object "OWNED" by someone else.
              2- In my case I am the owner of the object (i.e. pWebSkt), so using Qpointer would incur un-required overhead. (even if it has no/little over head I would like to know how this can be achieved without using QPointer to get a better understanding on the process of how objects are destroyed and cleaned in Qt)
              3- So I want to destroy/delete my resource(pWebSkt) when it gets disconnected and fires the corresponding signal. At this point I also expect to set the pointer to "nullptr".

              How can this be achieved? Would the following code work safely?

              connect(pWebSkt, SIGNAL(disconnected()), pWebSkt, SLOT(deleteLater());
              connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(onDisconnected());
              ....
              
              void MyClass::onDisconnected()
              {
              	pWebSkt = nullptr;
              }
              

              ** Is the second code snippet in my original post valid (i.e. deleting the pointer in the slot and then setting it to nullptr)?

              Thanks in advance

              VRoninV 1 Reply Last reply
              0
              • A alex_qwa

                @raven-worx thanks for your prompt response;

                Your answer solves my problem, BUT ...

                1- QPointer should be used when i need a pointer to an object "OWNED" by someone else.
                2- In my case I am the owner of the object (i.e. pWebSkt), so using Qpointer would incur un-required overhead. (even if it has no/little over head I would like to know how this can be achieved without using QPointer to get a better understanding on the process of how objects are destroyed and cleaned in Qt)
                3- So I want to destroy/delete my resource(pWebSkt) when it gets disconnected and fires the corresponding signal. At this point I also expect to set the pointer to "nullptr".

                How can this be achieved? Would the following code work safely?

                connect(pWebSkt, SIGNAL(disconnected()), pWebSkt, SLOT(deleteLater());
                connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(onDisconnected());
                ....
                
                void MyClass::onDisconnected()
                {
                	pWebSkt = nullptr;
                }
                

                ** Is the second code snippet in my original post valid (i.e. deleting the pointer in the slot and then setting it to nullptr)?

                Thanks in advance

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7
                1. You are effectively delegating pWebSkt to delete itself so you can lose ownership (as long as pWebSkt has also a parent)
                2. no overhead really, QPointer is basically a slot connected to the destroyed signal and nothing more
                3. your example works, you can, alternatively connect to QObject::destroyed but that's exactly what QPointer does

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                A 1 Reply Last reply
                3
                • VRoninV VRonin
                  1. You are effectively delegating pWebSkt to delete itself so you can lose ownership (as long as pWebSkt has also a parent)
                  2. no overhead really, QPointer is basically a slot connected to the destroyed signal and nothing more
                  3. your example works, you can, alternatively connect to QObject::destroyed but that's exactly what QPointer does
                  A Offline
                  A Offline
                  alex_qwa
                  wrote on last edited by
                  #8

                  @VRonin I am new to Qt, so sorrry if my questions are no good.
                  if i've understood correctly you are telling me that I can set

                  void MyClass::onDestroyed()
                  {
                  pWebSkt=nullptr
                  }
                  

                  Q: Then how can the object (which is set to nullptr) destroy its childs immediately after this signal is emmited as suggested by the documentation of "destroyed" signal

                  raven-worxR 1 Reply Last reply
                  0
                  • A alex_qwa

                    @VRonin I am new to Qt, so sorrry if my questions are no good.
                    if i've understood correctly you are telling me that I can set

                    void MyClass::onDestroyed()
                    {
                    pWebSkt=nullptr
                    }
                    

                    Q: Then how can the object (which is set to nullptr) destroy its childs immediately after this signal is emmited as suggested by the documentation of "destroyed" signal

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by raven-worx
                    #9

                    @alex_qwa said in How to set a pointer to an object to "nullptr" upon calling deleteLater():

                    Q: Then how can the object (which is set to nullptr) destroy its childs immediately after this signal is emmited as suggested by the documentation of "destroyed" signal

                    you just set your local pointer (which holds the address to memory) to null. The object still exists in the memory though and gets deleted once the control returns from your onDestroyed() slot.

                    So the pointer does, as the name says, only point to a memory address, but is not the object itself per se.
                    Hope its clearer now.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    A 1 Reply Last reply
                    4
                    • raven-worxR raven-worx

                      @alex_qwa said in How to set a pointer to an object to "nullptr" upon calling deleteLater():

                      Q: Then how can the object (which is set to nullptr) destroy its childs immediately after this signal is emmited as suggested by the documentation of "destroyed" signal

                      you just set your local pointer (which holds the address to memory) to null. The object still exists in the memory though and gets deleted once the control returns from your onDestroyed() slot.

                      So the pointer does, as the name says, only point to a memory address, but is not the object itself per se.
                      Hope its clearer now.

                      A Offline
                      A Offline
                      alex_qwa
                      wrote on last edited by
                      #10

                      @raven-worx thanks a lot

                      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