【发布时间】:2016-02-04 03:20:34
【问题描述】:
最后我安装了 Ubuntu 并设置了 Qt+Valgrind 以防止我在 Windows 中无法做到的内存泄漏。所以我不明白这段代码是否提供内存泄漏?事实上,Valgrind 说我只有 500 多个问题,但对泄漏一无所知。我
#include <QWidget>
#include <QFrame>
#include <QVBoxLayout>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget * wdgt = new QWidget; //this line should be the cause of leakage
//if it exist (as far as i know)
QVBoxLayout *layout = new QVBoxLayout;
QFrame * frame = new QFrame;
frame->setFrameStyle(QFrame::Panel | QFrame::Plain);
frame->setLineWidth(5);
layout->addWidget(frame);
wdgt->setLayout(layout);
wdgt->setFixedSize(800,600);
wdgt->show();
return a.exec();
}
【问题讨论】:
-
不,因为操作系统会处理它。
-
但在这种情况下它不是
int * a = new int; -
每个
new都应该有对应的delete。或者使用智能指针 -
@EdHeal 在 Qt 的情况下不是,如果对象有父对象,则父对象会销毁它
标签: c++ qt memory-leaks valgrind