简体   繁体   English

如何释放内存?

[英]How to release memory?

I have Created a simple QT application for my university assignment. 我已经为我的大学作业创建了一个简单的QT应用程序。 What i have done is pop up a new QManinWindow from a Above QMainWindow. 我所做的是从上方的QMainWindow弹出一个新的QManinWindow。 When i click a button from the main ui it will pop up a new QMainWindow object (Note Pad) 当我从主界面单击一个按钮时,它将弹出一个新的QMainWindow对象(记事本)

Note pad is also a QMainWindow object 记事本也是QMainWindow对象

My Problem is when I'm creating the object it takes some memory from the ram but when I'm closing it (pop up window) memory is not releasing. 我的问题是,当我创建对象时,它从ram占用了一些内存,但是当我关闭它(弹出窗口)时,内存却没有释放。 When each time I'm pressing a button memory is allocated but application does not relese the memory when im closing it. 每次按下按钮时,都会分配内存,但在关闭内存时应用程序不会占用内存。 Please check the main screen of the app. 请检查应用程序的主屏幕。

i just want to know how to release that memory. 我只想知道如何释放那个记忆。 I have tried so many things but nothing worked well. 我已经尝试了很多东西,但是没有任何效果。

I have set the closeEvent public on NotePad class and I listen the close event from main object when its get triggered i have deleted the poped up object. 我在NotePad类上设置了closeEvent公共类,当触发它时,我从主对象监听了close事件,我已经删除了弹出的对象。 But it cause ad unexpected stop. 但这会导致广告意外停止。

void MainWindow::on_notePadBtn_clicked()
{

    NotePad *notePad = new NotePad(this);
    notePad->raise();
    notePad->activateWindow();
    notePad->show();
}


NotePad::NotePad(QWidget *parent) :QMainWindow(parent),ui(new Ui::NotePad) {

    ui->setupUi(this);
    this->setWindowTitle("Note Pad");
}

You don't really need to override closeEvent , Qt has Qt::WA_DeleteOnClose attribute, that does exactly what you want, you can use it like this: 您实际上不需要重写closeEvent ,Qt具有Qt :: WA_DeleteOnClose属性,该属性正是您想要的,您可以像这样使用它:

//...
NotePad *notePad = new NotePad(this);
notePad->setAttribute(Qt::WA_DeleteOnClose);
notePad->raise();
notePad->activateWindow();
notePad->show();
//...

I'm not familiar with Qt. 我对Qt不熟悉。

But to my understanding if you use the operator new you must use delete (in a scope where you have access to the pointer created with new). 但是据我了解,如果您使用运算符new,则必须使用delete (在可以访问使用new创建的指针的范围内)。

Object *foo = new Object();
// Do stuff with foo...
delete foo;
// DO NOT use foo from now on. 

Hope that helps a bit, maybe. 希望可以有所帮助。 Like I said I'm not familiar with Qt so if you have doubts about how some features are implemented you should look at the their docs. 就像我说的那样,我对Qt并不熟悉,因此,如果您对某些功能的实现方式有疑问,则应查看其文档。

(cf: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf §3.7.4p63) (cf: http: //www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf§3.7.4p63)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM