2017-08-08 5 views
0

QWidgetQHBoxLayoutと2つのQPushButtonsを含む)カスタムを作成し、GUIのQVBoxLayoutに追加しました。このカスタムQWidget - オブジェクトが複数回作成され、削除されます(下のコード)。QLayout内でカスタムQWidgetsを作成および削除する際にRAMの問題が発生する

topをコンソール(埋め込みLinux)に入力すると、新しいQWidgetを追加するたびにRAMが増加します。それで大丈夫です!しかし、私は削除時にRAMの減少を見ることはできません。

私のコードで何が問題になっていますか?私は、RAMがカスタムの削除時に減少することを望みますQWidgets

myCustomWidget.h

class QCustomPushButton_withinIcon_LeftAndRight : public QWidget { 
    Q_OBJECT 

public: 
    //functions 
    QCustomPushButton_withinIcon_LeftAndRight(QWidget *parent = 0); 
    ~QCustomPushButton_withinIcon_LeftAndRight(); 

    //other slots like: 
    // - virtual void mousePressEvent(); 
    // - virtual void mouseReleaseEvent(); 
    //other signals like: 
    // - void clicked(); 
    //other functions like: 
    // - virtual void setEnabled(bool a); 
    // - virtual void paintEvent(QPaintEvent *); 
    // - ... 

private: 
    //variables 
    QLayout *innerLayout;   //!< Layout for two buttons 
    QPushButton *buttonLeft;  //!< Button left 
    QPushButton *buttonRight;  //!< Button right 

    //other variables like: 
    // - bool enabled; 
    // - ... 
}; 

myCustomWidget.cpp

QCustomPushButton_withinIcon_LeftAndRight::QCustomPushButton_withinIcon_LeftAndRight(QWidget *parent) : 
    QWidget(parent), 
    mSVG (new svg), 
    mGradient (new gradient) 
{ 
    enabled = true; 

    //create innerLayout 
    innerLayout = new QHBoxLayout(); 
    this->setLayout(innerLayout); 

    //create buttons 
    buttonLeft = new QPushButton(); 
    buttonRight = new QPushButton(); 
    innerLayout->addWidget(buttonLeft); 
    innerLayout->addWidget(buttonRight); 

    //create connections like: 
    // - connect (buttonLeft, SIGNAL(pressed()), this, SLOT(mousePressEvent())); 
    // - ... 

    //set some stylesheets 
    // - buttonLeft->setStyleSheet("..."); 
} 
QCustomPushButton_withinIcon_LeftAndRight::~QCustomPushButton_withinIcon_LeftAndRight() 
{ 
    //I think, that this is right. If not, correct me. 
    delete buttonLeft; 
    delete buttonRight; 
    delete innerLayout; 
} 

//void QCustomPushButton_withinIcon_LeftAndRight::mousePressEvent() {} 
//void QCustomPushButton_withinIcon_LeftAndRight::mouseReleaseEvent() {} 
//void QCustomPushButton_withinIcon_LeftAndRight::setEnabled(bool a) {} 
//void QCustomPushButton_withinIcon_LeftAndRight::paintEvent(QPaintEvent *) {} ... 

gui.cpp(GUIにQLayoutQWidgetsを追加)

QCustomPushButton_withinIcon_LeftAndRight *button = new QCustomPushButton_withinIcon_LeftAndRight(); 
    //add button to layout 
    parentUiWindow->aLayoutNameInGui->addWidget(button); 
    //aLayoutNameInGui is type of QVBoxLayout 

gui.cpp(QWidgetsを削除GUI内にあるQLayout

//delete all buttons in layout 
    QLayoutItem *child; 
    while((child = parentUiWindow->aLayoutNameInGui->layout()->takeAt(0)) != 0) { 
     //parentUiWindow->aLayoutNameInGui->removeWidget(child->widget()); //already removed by ->takeAt() 
     //child->widget()->setParent(NULL); 
     delete child->widget(); 
     delete child; 
    } 
+2

この問題に関しては、重複している必要があります。しかし、「問題」は、オペレーティングシステムが、メモリを「削除する」か、そうでなければ解放するときに、割り当てられたメモリページをプロセスからマップ解除する必要がない可能性が最も高いです。代わりに、それはいつでも同じように感じることができます。つまり、それらのページはメモリリークのように見えます。それは偽陽性です。 –

+0

"gui.cpp"(GUIのQLayoutにQWidgetsを追加する)の代わりにコードに関数宣言を入れると、コードを読み込んで複製する方が簡単です –

+1

'mSVG'と' mGradient'をあなたはどこにいてもこれらの物を破壊するのですか? – thuga

答えて

1

topコマンドでメモリ使用量を調べると、誤検出が発生する可能性があります。 で述べたように、一部のプログラマがの場合は、一部のオブジェクトでdeleteを呼び出すと、OSが割り当てられたメモリをプロセスから解放するとは限りません。

mSVG (new svg), 
mGradient (new gradient) 

をしかし、あなたは、これらのオブジェクトを破壊するように見えることはありません:

ただし、newとあなたのQCustomPushButton_withinIcon_LeftAndRightコンストラクタで2つのオブジェクトを作成しています。そこで、メモリリークが発生する可能性があります。

関連する問題