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. The destructor is not executed
Forum Updated to NodeBB v4.3 + New Features

The destructor is not executed

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 1.7k 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.
  • C Offline
    C Offline
    chris_rookie
    wrote on last edited by
    #1
    QSharedMemory shared(shared_name);
    if (shared.attach())
    {
        return 0;
    }
    shared.create(1);
    

    I write the code in my QMainWindow class.
    I close the Widget at once, next time, to open my QMainWindow, I should execute my executable twice.
    I found the QMainWindow's destructor not be executed.
    There must be something wrong in my program. But I gdb, nothing was be warned.

    JonBJ 1 Reply Last reply
    0
    • C chris_rookie
      QSharedMemory shared(shared_name);
      if (shared.attach())
      {
          return 0;
      }
      shared.create(1);
      

      I write the code in my QMainWindow class.
      I close the Widget at once, next time, to open my QMainWindow, I should execute my executable twice.
      I found the QMainWindow's destructor not be executed.
      There must be something wrong in my program. But I gdb, nothing was be warned.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @chris_rookie said in The destructor is not executed:

      I close the Widget at once

      What widget? Your code shows no widget.

      next time, to open my QMainWindow

      You are opening the/a main window multiple times? Where do you close it? Why are you expecting its destructor to be called?

      Show relevant code. Temporarily remove this QSharedMemory to see whether it has no effect on behaviour.

      1 Reply Last reply
      4
      • C Offline
        C Offline
        chris_rookie
        wrote on last edited by
        #3

        @JonB Sorry, My expression is not very clear.
        The Widget means my executable.
        I clicked the button close with the slot this->close(),
        then my executable exited.
        but it didn't execute the destructor.

        MainWindow::MainWindow(QWidget *parent) :
        CustomizeMainWindow(parent)
        ,ui(new Ui::MainWindow)
        {
        Init();

        }
        MainWindow::~MainWindow()
        {
        }

        I made a break point on ~MainWindow().

        It may caused my program do not release the shared memory.
        next time, when execute my executable, my program exit directly and release the shared memory.
        I execute again, It can be open.

        I checked my code and rewritten some warning code.
        There some slots in super class, but never called by it's subclass or itself.
        I removed those, and then it worked.
        I don't quite understand why ic cause this.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So how did you create your MainWindow object?

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

          C 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            So how did you create your MainWindow object?

            C Offline
            C Offline
            chris_rookie
            wrote on last edited by
            #5

            @Christian-Ehrlicher

            //main.cpp
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
            
                uid_t uid = getuid();
                QString shared_name = QString("unispy_config_tools_%1").arg(uid);
            
                QSharedMemory shared(shared_name);
                if (shared.attach())
                {
                    return 0;
                }
                shared.create(1);
            
                MainWindow w;
                w.show();
            
                int ret = a.exec();
                return ret;
            }
            
            //mainwindow.h
            class MainWindow : public CustomizeMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();
            
            private:
                void saveConfig(const QString &name, const QMap<QString, QString> &allChangedConfig);
                void Init();
                void InitStackWidget();
                void SetUpCustomizeUI();
                void SetUpLogo();
                void SetUpStackWidgetOptionButtonSlot();
                void SetUpCloseButton();
                void SetUpMinmizeWindowButton();
                void SetUpOptionButton();
                void ChangeVisibleWidget(int index);
                void RegisterSlots();
                void OnBtnStateChanged();
                void SetUpApplyButton();
                void SetStyleSheet();
            
            private:
                Ui::MainWindow *ui;
            }
            
            //mainwindow.cpp
            MainWindow::MainWindow(QWidget *parent) :
                                CustomizeMainWindow(parent)
                               ,ui(new Ui::MainWindow)
            {
                 Init();
            
            }
            
            MainWindow::~MainWindow()
            {
            }
            
            void MainWindow::Init()
            {
                ui->setupUi(this);
                SetUpCustomizeUI();
                InitStackWidget();
                RegisterSlots();
            }
            
            
            
            //mainwindow 's super class
            //customize_mainwindow.h
            #ifndef CUSTOMIZEMAINWINDOW_H
            #define CUSTOMIZEMAINWINDOW_H
            
            #include <QMouseEvent>
            #include <QMainWindow>
            
            class CustomizeMainWindow : public QMainWindow
            {
                Q_OBJECT
            public:
                explicit CustomizeMainWindow(QWidget *parent = 0);
                ~CustomizeMainWindow();
            
            private:
                void paintEvent(QPaintEvent *);
                void mousePressEvent(QMouseEvent *event);
                void mouseMoveEvent(QMouseEvent *event);
                void mouseReleaseEvent(QMouseEvent *event);
            private:
                QPoint m_last_mouse_position;
            
                bool m_move_widget_flag;
            };
            
            #endif // CUSTOMIZEMAINWINDOW_H
            
            
            //customize_mainwindow.cpp
            #include "customize_mainwindow.h"
            
            #include <QStyleOption>
            #include <QPainter>
            #include <QBrush>
            #include <QApplication>
            
            CustomizeMainWindow::CustomizeMainWindow(QWidget* parent):
                                                  QMainWindow(parent)
                                                 ,m_move_widget_flag(false)
            {
            
                QString styleSheet = "QMainWindow {border: 1px solid rgb(128,128,128);}";
                this->setStyleSheet(styleSheet);
                this->setWindowFlags(Qt::FramelessWindowHint);
            }
            
            CustomizeMainWindow::~CustomizeMainWindow()
            {
            }
            
            void CustomizeMainWindow::paintEvent(QPaintEvent *)
            {
                QStyleOption opt;
                opt.init(this);
                QPainter p(this);
                style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
            }
            
            void CustomizeMainWindow::mousePressEvent(QMouseEvent *event)
            {
                if(event->button() == Qt::LeftButton)
                {
                    m_last_mouse_position = event->globalPos();
                    m_move_widget_flag = true;
                }
            }
            
            void CustomizeMainWindow::mouseReleaseEvent(QMouseEvent *event)
            {
                if(event->button() == Qt::LeftButton)
                {
                    m_move_widget_flag = false;
                }
            }
            
            void CustomizeMainWindow::mouseMoveEvent(QMouseEvent *event)
            {
                if (!event->buttons().testFlag(Qt::LeftButton))
                        return;
                if(!m_move_widget_flag)
                        return;
                const QPoint position = pos() + event->globalPos() - m_last_mouse_position; //the position of mainfrmae + (current_mouse_position - last_mouse_position)
                move(position.x(), position.y());
                m_last_mouse_position = event->globalPos();
            }
            
            //void CustomizeMainWindow::on_button_minimize_cliked()
            //{
            //    setWindowState(windowState() | Qt::WindowMinimized);
            //    QApplication::processEvents();
            //    setWindowState(windowState() & ~Qt::WindowMinimized);
            //}
            

            There was a slot to close my mainwindow defined in the super class.
            The slot;s content is this->close();
            but, the mainwindow class never called it.
            My mainwindow contains a StackedWidget and some some widgets class int it.
            also, they extend a super class like customize_mainwindow that has a close slot,
            but never be called. I removed them all.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              According to your code the dtor of MainWindow is executed directly after 'return ret;'

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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                chris_rookie
                wrote on last edited by
                #7

                @Christian-Ehrlicher said in The destructor is not executed:

                dto

                I make a breaking pointer on the destructor.
                but it didn't blocked at the breaking point. why?

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @chris_rookie said in The destructor is not executed:

                  but it didn't blocked at the breaking point. why?

                  Put a qDebug() or something in the ctor.

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

                  C 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @chris_rookie said in The destructor is not executed:

                    but it didn't blocked at the breaking point. why?

                    Put a qDebug() or something in the ctor.

                    C Offline
                    C Offline
                    chris_rookie
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher

                    I did it.
                    I defined QSharedMemory as a poniter, and delete the poniter in the ctor.
                    it didn't work.
                    and why the QSharedMemory didn'y release the shared memory.^_^

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @chris_rookie said in The destructor is not executed:

                      I did it.
                      I defined QSharedMemory as a poniter, and delete the poniter in the ctor.

                      You should put a qDebug() statement in there to see if it is printed when you exit the application!

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

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

                        Hi,

                        Out of curiosity, are you trying to avoid having several instances of your application running at the same time ?

                        If so, there's the QtSingleApplication class for that in the QtSolution repo.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        C 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @chris_rookie said in The destructor is not executed:

                          I did it.
                          I defined QSharedMemory as a poniter, and delete the poniter in the ctor.

                          You should put a qDebug() statement in there to see if it is printed when you exit the application!

                          C Offline
                          C Offline
                          chris_rookie
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher
                          ok, I'll try it.
                          This is result of my test.
                          QMetaObject::connectSlotsByName: No matching signal for on_button_close_cliked()
                          my qDebug output get in the destory function.

                          Thank you. And I wanna ask why the QSharedMemory didn't release.

                          1 Reply Last reply
                          0
                          • SGaistS SGaist

                            Hi,

                            Out of curiosity, are you trying to avoid having several instances of your application running at the same time ?

                            If so, there's the QtSingleApplication class for that in the QtSolution repo.

                            C Offline
                            C Offline
                            chris_rookie
                            wrote on last edited by
                            #13

                            @SGaist
                            Yes. I wanna do it.
                            I'll read it.
                            Thank you.

                            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