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. multiple new ui classes need delete?
Forum Updated to NodeBB v4.3 + New Features

multiple new ui classes need delete?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 524 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.
  • P Offline
    P Offline
    Psnarf
    wrote on 8 Dec 2019, 17:32 last edited by
    #1

    I have multiple classes that I use to display different ui forms from push buttons on_click(). When I close the other class UI, that class destructor deletes the ui. Do I also need to call delete() to free those class instances allocated on the heap? Does the other class destructor take care of that?

     OtherClass *other1 = new OtherClass;
    other1->function(data);
    other1->show();
    //other stuff
    delete other1;
    }
    

    Also, when I create Qt classes on the heap, do they get deleted by their destructors?

    QImage *img = new QImage;
    
    P 1 Reply Last reply 8 Dec 2019, 17:41
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 8 Dec 2019, 17:41 last edited by
      #2

      Why should a pointer get deleted automatically somewhere when you're using a plain pointer and don't use Qt parent - child relationship?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      P 1 Reply Last reply 8 Dec 2019, 23:01
      2
      • P Psnarf
        8 Dec 2019, 17:32

        I have multiple classes that I use to display different ui forms from push buttons on_click(). When I close the other class UI, that class destructor deletes the ui. Do I also need to call delete() to free those class instances allocated on the heap? Does the other class destructor take care of that?

         OtherClass *other1 = new OtherClass;
        other1->function(data);
        other1->show();
        //other stuff
        delete other1;
        }
        

        Also, when I create Qt classes on the heap, do they get deleted by their destructors?

        QImage *img = new QImage;
        
        P Offline
        P Offline
        Pl45m4
        wrote on 8 Dec 2019, 17:41 last edited by
        #3

        @Psnarf

        If you allocate memory on heap by using new, you have to delete it yourself.
        BTW: In most cases, you dont have to allocate QImages on heap. If you create it on stack and fill it with data, it gets cleaned up when it goes out of scope. (But depends on your case.)


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        P 1 Reply Last reply 8 Dec 2019, 21:22
        2
        • P Pl45m4
          8 Dec 2019, 17:41

          @Psnarf

          If you allocate memory on heap by using new, you have to delete it yourself.
          BTW: In most cases, you dont have to allocate QImages on heap. If you create it on stack and fill it with data, it gets cleaned up when it goes out of scope. (But depends on your case.)

          P Offline
          P Offline
          Psnarf
          wrote on 8 Dec 2019, 21:22 last edited by
          #4

          @Christian-Ehrlicher
          Color me a Qt newbie. Don't know about Qt parent - child relationships.

          @Pl45m4
          Thanks for the new explanation. I'm using a lot of large images, don't want to exhaust the stack. If I pass them to another class for processing, more better to pass a pointer? Slowly working my way up to edge detection.
          Appreciate your help!

          1 Reply Last reply
          0
          • C Christian Ehrlicher
            8 Dec 2019, 17:41

            Why should a pointer get deleted automatically somewhere when you're using a plain pointer and don't use Qt parent - child relationship?

            P Offline
            P Offline
            Psnarf
            wrote on 8 Dec 2019, 23:01 last edited by
            #5

            @Christian-Ehrlicher

            I put all the new statements in my constructor, then delete them all in my destructor . That way I can keep track of the heap allocation/deallocation.
            other1 *other = new other1(this);
            I think that establishes the parent-child relation? Then other1's destructor gets called to delete its Ui. If I don't do that, the other1 structure remains on the heap as a memory leak when I delete its pointer?

            P 1 Reply Last reply 9 Dec 2019, 01:12
            0
            • P Psnarf
              8 Dec 2019, 23:01

              @Christian-Ehrlicher

              I put all the new statements in my constructor, then delete them all in my destructor . That way I can keep track of the heap allocation/deallocation.
              other1 *other = new other1(this);
              I think that establishes the parent-child relation? Then other1's destructor gets called to delete its Ui. If I don't do that, the other1 structure remains on the heap as a memory leak when I delete its pointer?

              P Offline
              P Offline
              Pl45m4
              wrote on 9 Dec 2019, 01:12 last edited by Pl45m4 12 Sept 2019, 01:17
              #6

              @Psnarf

              Yes, this is a possible solution.

              If your other1 class inherits QObject and you set this-class (I guess QMainWindow or any other std Qt class?!) as parent, other1 and even its children get cleaned up, when deleting this ( the parent)

              https://doc.qt.io/archives/qt-4.8/objecttrees.html#overview


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Psnarf
                wrote on 9 Dec 2019, 02:43 last edited by
                #7

                Obquote: "All I know is what I read in doc.qt.io."
                I learned that QImage is among the list of Implicitly Shared classes which can be passed to functions by value and returned from functions without concern for copying overhead. Must take care with STL-style iterators, though. I missed that feature when I first started using QImages. I'm going to re-write my trig utility classes as QObjects without Ui.
                https://doc.qt.io/qt-5/objecttrees.html

                Should it be of concern to anyone, QtMath trig functions take qreal arguments in radians, not degrees. Doc mentions the return values as radians, but not the arguments.
                Thanks again for your help! I'm now a little-smarter dummy.

                1 Reply Last reply
                0

                1/7

                8 Dec 2019, 17:32

                • Login

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