• 4101阅读
  • 3回复

感觉自己写的一不小心就内存泄露了,这个例子为什么不会内存泄露? [复制链接]

上一主题 下一主题
离线czhouwang
 

只看楼主 倒序阅读 楼主  发表于: 2010-04-28
ColorNamesDialog::ColorNamesDialog(QWidget *parent)
    : QDialog(parent)
{
    sourceModel = new QStringListModel(this);
    sourceModel->setStringList(QColor::colorNames());

    proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel(sourceModel);
    proxyModel->setFilterKeyColumn(0);

    listView = new QListView;
    listView->setModel(proxyModel);
    listView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    filterLabel = new QLabel(tr("&Filter:"));
    filterLineEdit = new QLineEdit;
    filterLabel->setBuddy(filterLineEdit);

    syntaxLabel = new QLabel(tr("&Pattern syntax:"));
    syntaxComboBox = new QComboBox;
    syntaxComboBox->addItem(tr("Regular expression"), QRegExp::RegExp);
    syntaxComboBox->addItem(tr("Wildcard"), QRegExp::Wildcard);
    syntaxComboBox->addItem(tr("Fixed string"), QRegExp::FixedString);
    syntaxLabel->setBuddy(syntaxComboBox);

    connect(filterLineEdit, SIGNAL(textChanged(const QString &)),
            this, SLOT(reapplyFilter()));
    connect(syntaxComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(reapplyFilter()));

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(listView, 0, 0, 1, 2);
    mainLayout->addWidget(filterLabel, 1, 0);
    mainLayout->addWidget(filterLineEdit, 1, 1);
    mainLayout->addWidget(syntaxLabel, 2, 0);
    mainLayout->addWidget(syntaxComboBox, 2, 1);
    setLayout(mainLayout);

    setWindowTitle(tr("Color Names"));
}



有些new出来的比如syntaxComboBox 没有delete掉,也没有指定parent。是不是  mainLayout->addWidget(filterLineEdit, 1, 1);之后 filterLineEdit就成了mainLayout的child了?

还有就是 QT,release出来的还是挺大的,没多少东西就好几兆,(跟MFC相比大很多),占得内存也占很多,这是为什么...
[ 此帖被czhouwang在2010-04-28 10:30重新编辑 ]
离线yangfanxing
只看该作者 1楼 发表于: 2010-04-28
1、自主释放的吧;

2、qDebug() << try filterLineEdit->parentWidget()->windowTitle();
PHPWind好恶心。。。不想看这种界面。。。
离线czhouwang

只看该作者 2楼 发表于: 2010-04-28
引用第1楼yangfanxing于2010-04-28 10:27发表的  :
1、自主释放的吧;
2、qDebug() << try filterLineEdit->parentWidget()->windowTitle();




你这句的意思是filterLineEdit的parent是windowTitle()么?
那就是
    mainLayout->addWidget(filterLineEdit, 1, 1);之后
    setLayout(mainLayout);
    setWindowTitle(tr("Color Names"));

   filterLineEdit就成了它的child?
离线czhouwang

只看该作者 3楼 发表于: 2010-04-28
然后打开一个对话框再关掉,内存就比原来的多了几K,为什么会多呢?能对话框里能delete掉的应该都delete掉了。现在总感觉怕怕的,感觉没怎样就泄露了...对内存管理总有种怀疑感,期待大牛解惑~~
快速回复
限100 字节
 
上一个 下一个