Memory problem and strange behaviour



  • Hi,

    I'm quite new with Qt, and I have some issues with my program. Some executions end without any errors, and others don't.

    Sometimes, the debug consol print a lot of lines, like there are a memory leak, sometimes the app stop promptly. I don't know why, I started to observ this behaviour after adding a layout on the "preference window".

    This is my code :
    main.cpp
    @
    #include <QtWidgets>
    #include <QLineEdit>
    #include <QToolButton>
    #include <MainWindow.h>

    int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
    }
    @
    MainWindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtWidgets>
    #include <QMenu>
    #include <QToolBar>
    #include <QTabWidget>
    #include <QAction>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    MainWindow();
    private slots:
    void newWindow();
    void openPreferencesMenu();
    private:
    /*
    QMenu *fileMenu;
    QToolBar *fileToolBar;
    QToolBar *editToolBar;
    */
    QMenu *fileMenu;
    QMenu *editMenu;

    QAction *newAct;
    QAction *editAct;
    

    };

    #endif // MAINWINDOW_H@

    PreferenceWindow.h
    @#define PREFERENCEWINDOW_H

    #include <QtWidgets>

    class PreferenceWindow : public QDialog
    {
    Q_OBJECT
    public:
    PreferenceWindow();
    private:
    QLineEdit *programPath;
    QLabel *programPathLabel;

    QLineEdit *programDefaultOptions;
    QLabel * programDefaultOptionsLabel;
    
    QFormLayout *layout;
    QPushButton *acceptButton;
    QPushButton *cancelButton;
    

    };

    #endif // PREFERENCEWINDOW_H@

    MainWIndow.cpp
    @#include <PreferenceWindow.h>
    MainWindow::MainWindow() {
    fileMenu = menuBar()->addMenu(tr("&File"));
    editMenu = menuBar()->addMenu(tr("&Edit"));
    newAct=new QAction(QIcon("images/new.png"), tr("&New"), this);
    newAct->setShortcuts(QKeySequence::New);
    newAct->setStatusTip(tr("Create a new file"));
    editAct = new QAction(tr("&Edit"), this);
    editAct->setShortcuts(QKeySequence::Preferences);
    editAct->setStatusTip(tr("Set preferences"));
    connect(newAct, SIGNAL(triggered()), this, SLOT(newWindow()));
    connect(editAct, SIGNAL(triggered()), this, SLOT(openPreferencesMenu()));
    fileMenu->addAction(newAct);
    editMenu->addAction(editAct);

    }

    void MainWindow::openPreferencesMenu() {
    PreferenceWindow *p = new PreferenceWindow();
    }

    void MainWindow::newWindow() {

    MainWindow *w=new MainWindow();
    w->setAttribute(Qt::WA_DeleteOnClose);
    w->move(x()-10,y()-10);
    w->show();
    

    }@

    PreferenceWindow.cpp
    @#include <PreferenceWindow.h>
    #include <QHBoxLayout>
    PreferenceWindow::PreferenceWindow() {

    this->setModal(true);
    programPath= new QLineEdit(this);
    programPathLabel=new QLabel(tr("program path : "), this);
    layout=new QFormLayout();
    
    programDefaultOptions=new QLineEdit(this);
    programDefaultOptionsLabel=new QLabel(tr("Default options :"),this);
    acceptButton = new QPushButton(tr("Accept"));
    cancelButton = new QPushButton(tr("Cancel"));
    
    layout->addRow(programPathLabel, programPath);
    layout->addRow(programDefaultOptionsLabel, programDefaultOptions);
    layout->addRow(acceptButton, cancelButton);
    
    this->setLayout(layout);
    connect(acceptButton, SIGNAL(clicked()), this, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    
    this->show();
    

    }
    @

    Thanks,

    Saroupille.



  • Create PreferenceWindow in MainWIndow.cpp like this:

    pwin = new PreferenceWindow();
    pwin->setHidden(true);

    and the SOLT: openPreferencesMenu()
    void MainWindow::openPreferencesMenu() {
    pwin->show();
    }



  • Thanks, it seems to work !

    But I don't really understand why my method caused this error. And why your method is better ?



  • the compiler should tell you
    with your code you get:
    warning: unused variable 'p' [-Wunused-variable]
    PreferenceWindow *p = new PreferenceWindow();
    ^
    you create the window as local variable



  • Indeed, thanks !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.