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. Check if a pointer was deleted in Qt for GUI Application

Check if a pointer was deleted in Qt for GUI Application

Scheduled Pinned Locked Moved Solved General and Desktop
pointerdelete
7 Posts 3 Posters 3.0k Views
  • 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.
  • S Offline
    S Offline
    SpaceToon
    wrote on last edited by
    #1

    Hi,

    I have this scenario: My GUI Application can search for Bluetooth Low Energy devices if the user hits the "scan" button.
    So I have something like this:

    //in my .h file
    QBluetoothDeviceDiscoveryAgent *discoveryAgent = nullptr;
    
    //In my .cpp file
    discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    

    I also have a reset button in my Application, which does something like clear Entry fields, disable buttons and so on. And in this Slot I also want to delete all Objects, so in my case discoveryAgent:

    void Test::reset()
    {
        if(discoveryAgent)
        {
            delete discoveryAgent;
        }
    }
    
    

    When I start my Programm and scan for devices, than all devices are listed. Then I click on my reset Button, and my object gets deleted (I think so, btw: how can I check if it is really deleted when debugging?).
    When i then press the reset button for the second time (makes no sense, but just for testing), my program crashes. So I think my if statement is not correct to check if a pointer is available or deleted. So is there (maybe a Qt-intern?) check for if a object stil exixts or if its ponter is deleted?

    K Pablo J. RoginaP 2 Replies Last reply
    0
    • S SpaceToon

      Hi,

      I have this scenario: My GUI Application can search for Bluetooth Low Energy devices if the user hits the "scan" button.
      So I have something like this:

      //in my .h file
      QBluetoothDeviceDiscoveryAgent *discoveryAgent = nullptr;
      
      //In my .cpp file
      discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
      

      I also have a reset button in my Application, which does something like clear Entry fields, disable buttons and so on. And in this Slot I also want to delete all Objects, so in my case discoveryAgent:

      void Test::reset()
      {
          if(discoveryAgent)
          {
              delete discoveryAgent;
          }
      }
      
      

      When I start my Programm and scan for devices, than all devices are listed. Then I click on my reset Button, and my object gets deleted (I think so, btw: how can I check if it is really deleted when debugging?).
      When i then press the reset button for the second time (makes no sense, but just for testing), my program crashes. So I think my if statement is not correct to check if a pointer is available or deleted. So is there (maybe a Qt-intern?) check for if a object stil exixts or if its ponter is deleted?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @SpaceToon

      That is not a Qt issue. It is simply standard C++.

      try

      void Test::reset()
      {
          if(discoveryAgent)
          {
              delete discoveryAgent;
              discoveryAgent = nullptr;
          }
      }
      

      Typically it is better in Qt to use deleteLater()

      Vote the answer(s) that helped you to solve your issue(s)

      S 1 Reply Last reply
      2
      • S SpaceToon

        Hi,

        I have this scenario: My GUI Application can search for Bluetooth Low Energy devices if the user hits the "scan" button.
        So I have something like this:

        //in my .h file
        QBluetoothDeviceDiscoveryAgent *discoveryAgent = nullptr;
        
        //In my .cpp file
        discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
        

        I also have a reset button in my Application, which does something like clear Entry fields, disable buttons and so on. And in this Slot I also want to delete all Objects, so in my case discoveryAgent:

        void Test::reset()
        {
            if(discoveryAgent)
            {
                delete discoveryAgent;
            }
        }
        
        

        When I start my Programm and scan for devices, than all devices are listed. Then I click on my reset Button, and my object gets deleted (I think so, btw: how can I check if it is really deleted when debugging?).
        When i then press the reset button for the second time (makes no sense, but just for testing), my program crashes. So I think my if statement is not correct to check if a pointer is available or deleted. So is there (maybe a Qt-intern?) check for if a object stil exixts or if its ponter is deleted?

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @SpaceToon I guess you have several things going on here.

        1. Why do you want to delete your QBluetoothDeviceDiscoveryAgent object when you do a reset?
          Isn't it suffice to clear all the other input fields, and then when the user fill them out again and press some (I assume) Discover button, go and ask the QBluetoothDeviceDiscoveryAgent object to start() a discovery again?
          If you have everything well laid out (i.e. deviceDiscovered() or finished() signals) you'll end up with the newly discovered device(s)
        2. Given that you do need to delete your QBluetoothDeviceDiscoveryAgent object, please do so as @koahnig suggested.

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • K koahnig

          @SpaceToon

          That is not a Qt issue. It is simply standard C++.

          try

          void Test::reset()
          {
              if(discoveryAgent)
              {
                  delete discoveryAgent;
                  discoveryAgent = nullptr;
              }
          }
          

          Typically it is better in Qt to use deleteLater()

          S Offline
          S Offline
          SpaceToon
          wrote on last edited by
          #4

          @koahnig said in Check if a pointer was deleted in Qt for GUI Application:

          @SpaceToon

          That is not a Qt issue. It is simply standard C++.

          try

          void Test::reset()
          {
              if(discoveryAgent)
              {
                  delete discoveryAgent;
                  discoveryAgent = nullptr;
              }
          }
          

          Typically it is better in Qt to use deleteLater()

          Thank you very much, that worked! Do you mean just replacing delete with deleteLater()?

          Why do you want to delete your QBluetoothDeviceDiscoveryAgent object when you do a reset?

          Isn't it suffice to clear all the other input fields, and then when the user fill them out again and press some (I assume) Discover button, go and ask the QBluetoothDeviceDiscoveryAgent object to start() a discovery again?

          If you have everything well laid out (i.e. deviceDiscovered() or finished() signals) you'll end up with the newly discovered device(s)

          @Pablo-J-Rogina
          Thank you for the question, I believe it has drawn my attention to a mistake in thinking. I thought that if I didn't delete "discoveryAgent" when reset is pressed , discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); would be called again. Here I thought that a new object is now being created, so that I then have two different QBluetoothDeviceDiscoveryAgent objects . But actually it is so that my object "deviceDiscoveryAgent" is simply overwritten if I do not delete it and discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); is called.

          So actually, I do not have to delete it at all.

          Pablo J. RoginaP K 2 Replies Last reply
          0
          • S SpaceToon

            @koahnig said in Check if a pointer was deleted in Qt for GUI Application:

            @SpaceToon

            That is not a Qt issue. It is simply standard C++.

            try

            void Test::reset()
            {
                if(discoveryAgent)
                {
                    delete discoveryAgent;
                    discoveryAgent = nullptr;
                }
            }
            

            Typically it is better in Qt to use deleteLater()

            Thank you very much, that worked! Do you mean just replacing delete with deleteLater()?

            Why do you want to delete your QBluetoothDeviceDiscoveryAgent object when you do a reset?

            Isn't it suffice to clear all the other input fields, and then when the user fill them out again and press some (I assume) Discover button, go and ask the QBluetoothDeviceDiscoveryAgent object to start() a discovery again?

            If you have everything well laid out (i.e. deviceDiscovered() or finished() signals) you'll end up with the newly discovered device(s)

            @Pablo-J-Rogina
            Thank you for the question, I believe it has drawn my attention to a mistake in thinking. I thought that if I didn't delete "discoveryAgent" when reset is pressed , discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); would be called again. Here I thought that a new object is now being created, so that I then have two different QBluetoothDeviceDiscoveryAgent objects . But actually it is so that my object "deviceDiscoveryAgent" is simply overwritten if I do not delete it and discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); is called.

            So actually, I do not have to delete it at all.

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @SpaceToon said in Check if a pointer was deleted in Qt for GUI Application:

            But actually it is so that my object "deviceDiscoveryAgent" is simply overwritten if I do not delete it and discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); is called.

            Wait!
            I was thinking of creating (i.e. new) a QBluetoothDeviceDiscoveryAgent object just once during the lifetime of your app, i.e. in the constructor of the object using that QBluetoothDeviceDiscoveryAgent

            But if you're are doing a discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); every time reset() is called, then you'll be piling up new objects without deleting them = memory leak!

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            S 1 Reply Last reply
            2
            • Pablo J. RoginaP Pablo J. Rogina

              @SpaceToon said in Check if a pointer was deleted in Qt for GUI Application:

              But actually it is so that my object "deviceDiscoveryAgent" is simply overwritten if I do not delete it and discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); is called.

              Wait!
              I was thinking of creating (i.e. new) a QBluetoothDeviceDiscoveryAgent object just once during the lifetime of your app, i.e. in the constructor of the object using that QBluetoothDeviceDiscoveryAgent

              But if you're are doing a discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); every time reset() is called, then you'll be piling up new objects without deleting them = memory leak!

              S Offline
              S Offline
              SpaceToon
              wrote on last edited by
              #6

              @Pablo-J-Rogina Ah okay, so I was right with my first thought. So I think your suggestions, of creating the object once in the constructor, is more suitable for my use case. Thank you again! I think I have to read a little bit more about C++ objects and memory allocation. Have a nice day :)

              1 Reply Last reply
              0
              • S SpaceToon

                @koahnig said in Check if a pointer was deleted in Qt for GUI Application:

                @SpaceToon

                That is not a Qt issue. It is simply standard C++.

                try

                void Test::reset()
                {
                    if(discoveryAgent)
                    {
                        delete discoveryAgent;
                        discoveryAgent = nullptr;
                    }
                }
                

                Typically it is better in Qt to use deleteLater()

                Thank you very much, that worked! Do you mean just replacing delete with deleteLater()?

                Why do you want to delete your QBluetoothDeviceDiscoveryAgent object when you do a reset?

                Isn't it suffice to clear all the other input fields, and then when the user fill them out again and press some (I assume) Discover button, go and ask the QBluetoothDeviceDiscoveryAgent object to start() a discovery again?

                If you have everything well laid out (i.e. deviceDiscovered() or finished() signals) you'll end up with the newly discovered device(s)

                @Pablo-J-Rogina
                Thank you for the question, I believe it has drawn my attention to a mistake in thinking. I thought that if I didn't delete "discoveryAgent" when reset is pressed , discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); would be called again. Here I thought that a new object is now being created, so that I then have two different QBluetoothDeviceDiscoveryAgent objects . But actually it is so that my object "deviceDiscoveryAgent" is simply overwritten if I do not delete it and discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); is called.

                So actually, I do not have to delete it at all.

                K Offline
                K Offline
                koahnig
                wrote on last edited by koahnig
                #7

                @SpaceToon said in Check if a pointer was deleted in Qt for GUI Application:

                Thank you very much, that worked! Do you mean just replacing delete with deleteLater()?

                It would be

                discoveryAgent->deleteLater();
                

                Check out the documentation under the given link in my post.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                2

                • Login

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