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 Update on Monday, May 27th 2025

multiple new ui classes need delete?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 506 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.
  • PsnarfP Offline
    PsnarfP Offline
    Psnarf
    wrote on 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;
    
    Pl45m4P 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

      PsnarfP 1 Reply Last reply
      2
      • PsnarfP Psnarf

        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;
        
        Pl45m4P Online
        Pl45m4P Online
        Pl45m4
        wrote on 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

        PsnarfP 1 Reply Last reply
        2
        • Pl45m4P Pl45m4

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

          PsnarfP Offline
          PsnarfP Offline
          Psnarf
          wrote on 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
          • Christian EhrlicherC Christian Ehrlicher

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

            PsnarfP Offline
            PsnarfP Offline
            Psnarf
            wrote on 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?

            Pl45m4P 1 Reply Last reply
            0
            • PsnarfP Psnarf

              @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?

              Pl45m4P Online
              Pl45m4P Online
              Pl45m4
              wrote on last edited by Pl45m4
              #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
              • PsnarfP Offline
                PsnarfP Offline
                Psnarf
                wrote on 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

                • Login

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