imx6平臺qt程序中dynamic_cast導致的內存增加

問題背景:imx6dl的主板,3.0的內核,busybox的文件系統 qt版本5.4.1

問題描述:如下程序運行中,未定義MEMTEST會出現計數300+時內存增加現象(無論是delete還是使用deletelater方法);在定義MEMTEST後的程序不會出現此問題,一切正常

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
#ifdef MEMTEST
    f1 = NULL;
    f2 = NULL;
    c1 = 0;
    c2 = 0;
//    connect(m_timer, SIGNAL(timeout()), this, SLOT(on_pushButton_2_clicked()));
    QObject::startTimer(1000);
    on_pushButton_clicked();
#else

    m_form = NULL;

    m_timer = new QTimer();
    m_timer->setInterval(1000);
    m_timer->start();
    connect(m_timer, SIGNAL(timeout()), this, SLOT(timer_out()));
#endif

}

MainWindow::~MainWindow()
{
    delete ui;
}
#ifdef MEMTEST
void MainWindow::on_pushButton_clicked()
{
    if(f1 == NULL)
    {
        Form1 *tmp_1 = new Form1();
        tmp_1->set_text(QString("PAGE 1 @@@@@@@@@") + QString::number(c1++));
        f1 = tmp_1;
        this->ui->verticalLayout->addWidget(tmp_1);
        tmp_1->show();
    }
    else
    {
        Form1 *tmp_1;
        tmp_1 = f1;
        tmp_1->close();
//        delete tmp_1;
        tmp_1->deleteLater();
        f1 = NULL;
    }
    qDebug() << c1 << "@@@@@@@@@@@@@@";
}

void MainWindow::on_pushButton_2_clicked()
{
    if(f2 == NULL)
    {
        Form2 *tmp_2 = new Form2();
        tmp_2->set_text(QString("PAGE 2 @@@@@@@@@") + QString::number(c2++));
        f2 = tmp_2;
        this->ui->verticalLayout->addWidget(tmp_2);
    }
    else
    {
        Form2 *tmp_2;
        tmp_2 = f2;
//        delete tmp_2;
        tmp_2->deleteLater();
        f2 = NULL;
    }
    qDebug() << c2;
}



void MainWindow::timerEvent(QTimerEvent *event)
{
    on_pushButton_clicked();
    on_pushButton_2_clicked();
}
#else
void MainWindow::timer_out()
{
    static int count = 0;
    if(count++ > 800)
    {
        m_timer->stop();
        return;
    }
    qDebug() << count;
    if( m_form == NULL)
    {
        m_form = new Form1();
    }
    else
    {
        if(dynamic_cast<Form1*>(m_form) != NULL)
        {
            Form1 *tmp = dynamic_cast<Form1*>(m_form);
            tmp->close();
//            tmp->deleteLater();
            delete tmp;

            m_form = new Form2();
            m_form->show();
        }
        else if(dynamic_cast<Form2*>(m_form) != NULL)
        {
            Form2 *tmp = dynamic_cast<Form2*>(m_form);
            tmp->close();
//            tmp->deleteLater();
            delete tmp;

            m_form = new Form1();
            m_form->show();
        }
        else
        {

        }

    }
}
#endif

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTime>
#include <QTimer>

#include "form1.h"
#include "form2.h"

#define MEMTEST

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

//    void delaysec(int sec);

private slots:
#ifdef MEMTEST
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();
#else
    void timer_out();
#endif

private:
    Ui::MainWindow *ui;
#ifdef MEMTEST
    Form1 *f1;
    Form2 *f2;
    int c1;
    int c2;
    int tmp;
    void timerEvent(QTimerEvent *event);
#else
    QTimer *m_timer;
    QWidget *m_form;
#endif

};

#endif // MAINWINDOW_H

出現以上問題,經過我們調試,得出解決方法爲:在m_form = new Form2();或m_form = new Form1();修改爲m_form = new Form1(this);和m_form = new Form2(this);在new時添加父對象指針

具體原因還需要後續明確

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章