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. Should I delete dynamic element or not in this code?
QtWS25 Last Chance

Should I delete dynamic element or not in this code?

Scheduled Pinned Locked Moved Unsolved General and Desktop
mainwindowdynamic allocatdynamic delegatpointers
7 Posts 4 Posters 2.7k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    In MainWindow class there is a pointer which is used for creating new window. In that way, should I delete this memory in ~MainWindow() destrcutor? Take and look:

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "anotherwindow.h"
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButtonLogin_clicked();
    
    private:
        Ui::MainWindow *ui;
        AnotherWindow *newWindow;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    // Hide current window and open new window
    void MainWindow::on_pushButtonLogin_clicked()
    {
        hide();
        newWindow = new AnotherWindow();
        newWindow->show();
    }
    
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! You don't need to call delete on the window: Just give it a parent and then the parent will take care of it. Example code can be found here.

      ? 1 Reply Last reply
      1
      • ? A Former User

        Hi! You don't need to call delete on the window: Just give it a parent and then the parent will take care of it. Example code can be found here.

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @Wieland Ok, but what if I can't use this as parent? Should I free it then?

        ? 1 Reply Last reply
        0
        • ? A Former User

          @Wieland Ok, but what if I can't use this as parent? Should I free it then?

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @testerius Yes, otherwise you'll leak memory.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            Oleksandr Malyushytskyy
            wrote on last edited by Oleksandr Malyushytskyy
            #5

            Direct and right answer which may lead nowhere:
            You need to delete every object you allocate with operator new, except if it is QObject subclass and it has a parent. Widgets subclasses may have also delete themselves depending on the flags set when closed.

            But the problem is not in what to delete, problem is in your design - It does not make sense to create Another window, store the pointer to it in MainWindow then delete MainWindow but try to keep AnotherWindow.
            AnotherWindow should be created at least at the same level as MainWindow constructor is called.

            VRoninV 1 Reply Last reply
            2
            • O Oleksandr Malyushytskyy

              Direct and right answer which may lead nowhere:
              You need to delete every object you allocate with operator new, except if it is QObject subclass and it has a parent. Widgets subclasses may have also delete themselves depending on the flags set when closed.

              But the problem is not in what to delete, problem is in your design - It does not make sense to create Another window, store the pointer to it in MainWindow then delete MainWindow but try to keep AnotherWindow.
              AnotherWindow should be created at least at the same level as MainWindow constructor is called.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @Oleksandr-Malyushytskyy said in Should I delete dynamic element or not in this code?:

              But the problem is not in what to delete, problem is in your design - It does not make sense to create Another window, store the pointer to it in MainWindow then delete MainWindow but try to keep AnotherWindow.
              AnotherWindow should be created at least at the same level as MainWindow constructor is called.

              Amen Brother

              "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

              1 Reply Last reply
              1
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Thanks guys for replies, generally my app isn't well designed because it's for education purpose. Anyway thank you very much.

                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