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

Memory Management

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.1k 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.
  • R Offline
    R Offline
    ramasamy.eeegmail.com
    wrote on last edited by
    #1

    Memory Management:

    I have little clarification about the memory management.

    I have two form with class name as (class first, class second). I placed button in first form to open second form.

    mSecond = new second();
    mSecond->show();

    1. Whenever am clicking this button, second form is opening. But every time it creating new object for the second form. how to delete the new object after closing the second form?

    2. In first form, how to identify that the second form is closed?

    plz suggest the right way to do.

    thanks
    ram
    @
    ===== main.c =======
    #include <QtGui/QApplication>
    #include "first.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    first mFirst;
    mFirst.show();
    return a.exec();
    }

    ==== first.c========

    #include "first.h"
    #include "ui_first.h"

    first::first(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::first)
    {
    ui->setupUi(this);
    }

    first::~first()
    {
    delete ui;
    }

    void first::on_pushButton_clicked()
    {

    mSecond = new second();
    mSecond->show();
    

    }

    ==== first.h =========

    #ifndef FIRST_H
    #define FIRST_H

    #include <QWidget>
    #include "second.h"

    namespace Ui {
    class first;
    }

    class first : public QWidget
    {
    Q_OBJECT

    public:
    explicit first(QWidget *parent = 0);
    ~first();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::first *ui;
    second *mSecond;
    };

    #endif // FIRST_H

    //===
    @

    Thanks & Regards,
    RAM :)

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Please use '@' tags for code.
      @
      void first::on_pushButton_clicked()
      {
      if (!mSecond)
      mSecond = new second(this);

      mSecond->show(); }
      @

      Your second is class member, so it's remembered as long as First is valid. No need to call "new" all the time. Also, if you want second to be deleted automatically when First is deleted, add it to QObject hierarchy by assigning the parent (new second(this), or ,Second->setParent(this)).

      (Z(:^

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ramasamy.eeegmail.com
        wrote on last edited by
        #3

        Hi sierdzio,

        I tried this but the application crashed

        output:
        the program has unexpectedly finished.
        C:\Qt\KeyMain\debug\KeyMain.exe exited with code -1073741819

        Thanks & Regards,
        RAM :)

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Add this to your constructor:
          @
          first::first(QWidget *parent) : QWidget(parent), ui(new Ui::first)
          {
          ui->setupUi(this);
          mSecond = NULL;
          }
          @

          If it still fails, debug the bastard. And yes, you still should update your first post with '@' tags.

          (Z(:^

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

            I would say: mSecond uninitialized in constructor (the pointers are not NULL by default)

            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
            0
            • R Offline
              R Offline
              ramasamy.eeegmail.com
              wrote on last edited by
              #6

              Hi Sierdzio & SGaist,

              Thanks for great work. Just assigned "mSecond = NULL;" in constructor it worked perfectly.

              Thanks & Regards,
              RAM :)

              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