Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Qt 4.8.7 memory artefacts



  • I need to use Qt 4.8.7 for Embedded Linux to create a very simple project for testing.
    Project includes 2 forms - Dialog (consists of QTextEdit and button. Button clears
    QTextEdit and QClipboard) and MainWindow (consists of button that exec Dialog).

    I edit text, copy part of it. Then clear editor and clipboard. I can't paste text after that,
    but I dump memory using gcore and see text that I copied (several copies in html form).
    What are these objects left in memory? Are these temporary objects or memory leaks?
    Or I use something incorrectly.

    My code:
    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
        class MainWindow;
    }
    
    class Dialog;
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        MainWindow(QWidget * parent = 0);
        virtual ~MainWindow();
    
    public slots:
        void slotOpenEditor();
        void slotDeleteEditor();
    
    private:
        Ui::MainWindow * ui;
        Dialog * dlg;
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "widget.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent),
          ui( new Ui::MainWindow ),
          dlg( 0 )
    {
        ui->setupUi(this);
        connect(ui->openEditor, SIGNAL(clicked()), this, SLOT(slotOpenEditor()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    
        delete dlg;
        dlg = 0;
    }
    
    void MainWindow::slotOpenEditor()
    {
        dlg = new Dialog();
        connect(dlg, SIGNAL(finished(int)), this, SLOT(slotDeleteEditor()));
        dlg->exec();
    }
    
    void MainWindow::slotDeleteEditor()
    {
        disconnect(dlg, SIGNAL(finished(int)), this, SLOT(slotDeleteEditor()));
        delete dlg;
        dlg = 0;
    }
    

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QDialog>
    
    namespace Ui {
        class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    public:
        Dialog(QWidget * parent = 0);
        virtual ~Dialog();
    public slots:
        void slotCleanClipboard();
    private:
        Ui::Dialog * ui;
    };
    
    #endif // WIDGET_H
    

    widget.cpp

    #include "widget.h"
    #include "ui_Dialog.h"
    
    #include <QDebug>
    #include <QClipboard>
    #include <QObject>
    
    Dialog::Dialog(QWidget *parent)
        : QDialog(parent),
          ui( new Ui::Dialog )
    {
        ui->setupUi(this);
        connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(slotCleanClipboard()));
        connect(ui->clearButton, SIGNAL(clicked()), ui->textEdit, SLOT(clear()));
    }
    
    void Dialog::slotCleanClipboard()
    {
        QApplication::clipboard()->clear();
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    

    main.cpp

    #include <QtGui>
    
    #include "mainwindow.h"
    
    int main(int argc, char** argv)
    {
        QApplication app(argc, argv);
        MainWindow mainWindow;
    
        mainWindow.show();
    
        return app.exec();
    }
    

    Thanks for answers.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    AFAIK, unless you specifically clean a memory region before releasing it, it will contain the "old data" until it gets overwritten.


Log in to reply