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 Release Memory ??
Forum Updated to NodeBB v4.3 + New Features

How To Release Memory ??

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 3.9k Views 2 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.
  • L Offline
    L Offline
    Lakith
    wrote on last edited by
    #1

    I have Created a simple QT application for my university assignment. What i have done is pop up a new QManinWindow from a Above QMainWindow. Check the below code you will figure it out

    0_1546287183648_Capture.PNG

    Note pad is also a QMainWindow object

    0_1546287210898_Capture2.PNG

    My Problem is when I'm creating the object it takes some memory from the ram but when I'm closing it (pop up window) memory is not releasing. When each time I'm pressing a button memory is allocated but application does not relese the memory when im closing it. Please check the main screen of the app.

    i just want to know how to release that memory. I have tried so many things but nothing worked well.

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

      Hi and welcome to devnet,

      If you don't reuse that widget, you can use the Qt::WA_DeleteOnClose which will trigger the deletion of the widget when you close it.

      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
      8
      • L Lakith

        I have Created a simple QT application for my university assignment. What i have done is pop up a new QManinWindow from a Above QMainWindow. Check the below code you will figure it out

        0_1546287183648_Capture.PNG

        Note pad is also a QMainWindow object

        0_1546287210898_Capture2.PNG

        My Problem is when I'm creating the object it takes some memory from the ram but when I'm closing it (pop up window) memory is not releasing. When each time I'm pressing a button memory is allocated but application does not relese the memory when im closing it. Please check the main screen of the app.

        i just want to know how to release that memory. I have tried so many things but nothing worked well.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @Lakith

        You could create your notepad outside the button event (below the line where your MainWindow UI is set). Then you have only one instance of your notepad window. OnButtonClick you can show/execute or hide it and delete it with you MainWindow destructor.
        In this approach the same window shows up, every time you click the button (disadvantage: If you change something in your NotePadWindow, for example a slider position, and close the window, the slider is still at this position when you reopen it).

        mainwindow.cpp

        // Inside MainWindow Constructor
        NotePad *m_notepad = new NotePad(this);
        // ...
        // ...
        // onButton Click / Open NotePadWindow
        {
           m_notepad->exec();
        }
        
        
        // MainWindow destructor
        {
            delete m_notepad;
            delete ui;
        }
        

        Or you allocate your NotePad on stack, if you are not planning to use the NotePad somewhere else. The memory gets freed automatically, when the objects' destructor is called (by X'ing the Window or Clicking a Close Btn).
        This creates a new window with defaults, when the button is clicked.

        {
        NotePad notepad(this);
        notepad.exec(); // or notepad.show();
        }
        

        Everytime you allocate memory on your heap (with the "new" keyword), you have to make sure, that you delete it, when it's no longer needed.


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

        ~E. W. Dijkstra

        L 1 Reply Last reply
        1
        • Pl45m4P Pl45m4

          @Lakith

          You could create your notepad outside the button event (below the line where your MainWindow UI is set). Then you have only one instance of your notepad window. OnButtonClick you can show/execute or hide it and delete it with you MainWindow destructor.
          In this approach the same window shows up, every time you click the button (disadvantage: If you change something in your NotePadWindow, for example a slider position, and close the window, the slider is still at this position when you reopen it).

          mainwindow.cpp

          // Inside MainWindow Constructor
          NotePad *m_notepad = new NotePad(this);
          // ...
          // ...
          // onButton Click / Open NotePadWindow
          {
             m_notepad->exec();
          }
          
          
          // MainWindow destructor
          {
              delete m_notepad;
              delete ui;
          }
          

          Or you allocate your NotePad on stack, if you are not planning to use the NotePad somewhere else. The memory gets freed automatically, when the objects' destructor is called (by X'ing the Window or Clicking a Close Btn).
          This creates a new window with defaults, when the button is clicked.

          {
          NotePad notepad(this);
          notepad.exec(); // or notepad.show();
          }
          

          Everytime you allocate memory on your heap (with the "new" keyword), you have to make sure, that you delete it, when it's no longer needed.

          L Offline
          L Offline
          Lakith
          wrote on last edited by
          #4

          @Pl45m4

          i hate tried the last thing that you specified mate. But once i do it and click the button, pop up windows(note pad) pops up and get killed.

          That means when i click the button to show the notepad it shows up and get killed instantly.

          jsulmJ Pl45m4P 2 Replies Last reply
          0
          • L Lakith

            @Pl45m4

            i hate tried the last thing that you specified mate. But once i do it and click the button, pop up windows(note pad) pops up and get killed.

            That means when i click the button to show the notepad it shows up and get killed instantly.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @Lakith Don't set your main window as parent of NotePad: when parent is deleted the child is deleted as well!
            See http://doc.qt.io/qt-5/objecttrees.html

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            L 1 Reply Last reply
            4
            • jsulmJ jsulm

              @Lakith Don't set your main window as parent of NotePad: when parent is deleted the child is deleted as well!
              See http://doc.qt.io/qt-5/objecttrees.html

              L Offline
              L Offline
              Lakith
              wrote on last edited by
              #6

              @jsulm said in How To Release Memory ??:

              http://doc.qt.io/qt-5/objecttrees.html

              Thanks mate :D
              Thank you All !!
              My problem Got solved

              1 Reply Last reply
              1
              • L Lakith

                @Pl45m4

                i hate tried the last thing that you specified mate. But once i do it and click the button, pop up windows(note pad) pops up and get killed.

                That means when i click the button to show the notepad it shows up and get killed instantly.

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @Lakith said in How To Release Memory ??:

                @Pl45m4

                i hate tried the last thing that you specified mate. But once i do it and click the button, pop up windows(note pad) pops up and get killed.

                That means when i click the button to show the notepad it shows up and get killed instantly.

                I guess you used "show()" instead of "exec()"?!

                "show()" lets your NotePad Window appear and continues with main loop routine... and because there is nothing more to do inside your buttonClick function, your NotePad is freed instantly and disappears.
                "Exec()" stops your main loop and creates a new one inside your NotePad Window. Your main program will continue, if you close the NotePad.

                Why are you using two QMainWindow-type classes? QMainWIndow provides additional menu- / toolbars per default... If you dont need them in your NotePadWindow, declaring at as a QDialog or QWindow would make things easier.


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

                ~E. W. Dijkstra

                jsulmJ 1 Reply Last reply
                1
                • Pl45m4P Pl45m4

                  @Lakith said in How To Release Memory ??:

                  @Pl45m4

                  i hate tried the last thing that you specified mate. But once i do it and click the button, pop up windows(note pad) pops up and get killed.

                  That means when i click the button to show the notepad it shows up and get killed instantly.

                  I guess you used "show()" instead of "exec()"?!

                  "show()" lets your NotePad Window appear and continues with main loop routine... and because there is nothing more to do inside your buttonClick function, your NotePad is freed instantly and disappears.
                  "Exec()" stops your main loop and creates a new one inside your NotePad Window. Your main program will continue, if you close the NotePad.

                  Why are you using two QMainWindow-type classes? QMainWIndow provides additional menu- / toolbars per default... If you dont need them in your NotePadWindow, declaring at as a QDialog or QWindow would make things easier.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Pl45m4 said in How To Release Memory ??:

                  your NotePad is freed instantly and disappears

                  This is not the case here as he allocates NotePad on the heap.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Pl45m4P 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @Pl45m4 said in How To Release Memory ??:

                    your NotePad is freed instantly and disappears

                    This is not the case here as he allocates NotePad on the heap.

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #9

                    @jsulm

                    Yes, but I if you create it on stack :)

                    Tried to show him other ways to allocate space for the NotePad class


                    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

                    • Login

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