Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

The destructor is not executed



  • 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.



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



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


  • Lifetime Qt Champion

    So how did you create your MainWindow object?



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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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



  • @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.^_^


  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

    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.



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



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


Log in to reply