• 7077阅读
  • 7回复

为什么不能够delete new出来的对象呢? [复制链接]

上一主题 下一主题
离线cn_q_t
 
只看楼主 倒序阅读 楼主  发表于: 2008-07-02
— 本帖被 XChinux 执行加亮操作(2008-07-02) —
我这个例子是《C++ GUI Programming with Qt 4》第一版里面的例子,我自己增加的析构函数。请看代码:
findDialog.h:
  1. #ifndef _FINDDIALOG_H_
  2. #define _FINDDIALOG_H_
  3. #include <QDialog>
  4. class QCheckBox;
  5. class QLabel;
  6. class QLineEdit;
  7. class QPushButton;
  8. class FindDialog : public QDialog
  9. {
  10.     Q_OBJECT
  11. public:
  12.     FindDialog(QWidget *parent = 0);
  13.     ~FindDialog();
  14. signals:
  15.     void findNext(const QString &str, Qt::CaseSensitivity cs);
  16.     void findPrevious(const QString &str, Qt::CaseSensitivity cs);
  17.    
  18. private slots:
  19.     void findClicked();
  20.     void enableFindButton(const QString &text);   
  21.    
  22. private:
  23.     QLabel  *label;
  24.     QLineEdit *lineEdit;
  25.     QCheckBox *caseCheckBox;
  26.     QCheckBox *backwardCheckBox;
  27.     QPushButton *findButton;
  28.     QPushButton *closeButton;   
  29. };
  30. #endif


而后是findDialog.cpp:
  1. #include <QtGui>
  2. #include "findDialog.h"
  3. FindDialog::~FindDialog()
  4. {
  5.     delete label;
  6.     if(label != NULL)
  7.     {
  8.         label = NULL;
  9.     }
  10.    
  11.     delete lineEdit;
  12.     if(lineEdit != NULL)
  13.     {
  14.         lineEdit = NULL;
  15.     }
  16.    
  17.     delete caseCheckBox;
  18.     if(caseCheckBox != NULL)
  19.     {
  20.         caseCheckBox = NULL;
  21.     }
  22.     delete backwardCheckBox;
  23.     if(backwardCheckBox != NULL)
  24.     {
  25.         backwardCheckBox = NULL;
  26.     }
  27.     delete findButton;
  28.     if(findButton != NULL)
  29.     {
  30.         findButton = NULL;
  31.     }
  32. }
  33. FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
  34. {
  35.     label = new QLabel(tr("Find &what:"));
  36.     lineEdit = new QLineEdit;
  37.     label->setBuddy(lineEdit);
  38.     caseCheckBox = new QCheckBox(tr("Match &case"));
  39.     backwardCheckBox = new QCheckBox(tr("Search &backward"));
  40.     findButton = new QPushButton(tr("&Find"));
  41.     findButton->setDefault(true);
  42.     findButton->setEnabled(false);
  43.    
  44.     closeButton = new QPushButton(tr("Close"));
  45.    
  46.     connect(lineEdit, SIGNAL(textChanged(const QString &)),
  47.             this, SLOT(enableFindButton(const QString &)));
  48.     connect(findButton, SIGNAL(clicked()),
  49.             this, SLOT(findClicked()));
  50.     connect(closeButton, SIGNAL(clicked()),
  51.             this, SLOT(close()));
  52.            
  53.     QHBoxLayout *topLeftLayout = new QHBoxLayout;
  54.     topLeftLayout->addWidget(label);
  55.     topLeftLayout->addWidget(lineEdit);
  56.        
  57.     QVBoxLayout *leftLayout = new QVBoxLayout;
  58.     leftLayout->addLayout(topLeftLayout);
  59.     leftLayout->addWidget(caseCheckBox);
  60.     leftLayout->addWidget(backwardCheckBox);
  61.    
  62.     QVBoxLayout *rightLayout = new QVBoxLayout;
  63.     rightLayout->addWidget(findButton);
  64.     rightLayout->addWidget(closeButton);
  65.     rightLayout->addStretch();
  66.    
  67.     QHBoxLayout *mainLayout = new QHBoxLayout;
  68.     mainLayout->addLayout(leftLayout);
  69.     mainLayout->addLayout(rightLayout);
  70.     setLayout(mainLayout);
  71.    
  72.     /********************
  73.     delete topLeftLayout;
  74.     if(topLeftLayout != NULL)
  75.     {
  76.         topLeftLayout = NULL;
  77.     }
  78.     **********/
  79.    
  80.     setWindowTitle(tr("Find"));
  81.     setFixedHeight(sizeHint().height());
  82. }
  83. void FindDialog::findClicked()
  84. {
  85.     QString text = lineEdit->text();
  86.     Qt::CaseSensitivity cs =
  87.     caseCheckBox->isChecked() ? Qt::CaseSensitive
  88.                 : Qt::CaseInsensitive;
  89.     if (backwardCheckBox->isChecked()) {
  90.         emit findPrevious(text, cs);
  91.     } else {
  92.         emit findNext(text, cs);
  93.     }
  94. }
  95. void FindDialog::enableFindButton(const QString &text)
  96. {
  97.     findButton->setEnabled(!text.isEmpty());
  98. }


最后main.cpp:
  1. #include <QApplication>
  2. #include "finddialog.h"
  3. int main(int argc, char *argv[])
  4. {
  5.     QApplication app(argc, argv);
  6.     FindDialog *dialog = new FindDialog;
  7.     dialog->show();
  8.     //delete dialog;
  9.     return app.exec();
  10. }


整个pro编译是没有问题的。
疑问在下面两个地方,依照我们学习C++的思想,每当new出来一个对象后,使用该对象完毕需要delete掉。为啥在他们提供的例子里面:
第一个,他们在构造函数里面new出来的对象,并没有在析构函数里面delete(该类的析构函数内容是我填写的)。
第二个,在构造函数里面new出来的那几个QHBoxLayout对象,在结束的时候不能够delete掉,delete掉后,运行起来该布局就不见了。
第三个,在main.cpp里面,new出来的dialog对象,当show()完后,为啥不能够delete掉?因为当delete掉后,整个窗体都不出来了。

P.S.:我是在winxp上安装的QT4.4,然后使用QT命令行运行的该程序。

谢谢!
离线mumutouv

只看该作者 1楼 发表于: 2008-07-02
自动delete掉了,你如果看看源码知道了,放置在widget上面的控件都会自动delete的
离线cn_q_t
只看该作者 2楼 发表于: 2008-07-02
如果在嵌入式系统里面呢,GC有那么及时吗?
请给出关于QT GC和Java GC的区别的例子。
离线浪漫天使
只看该作者 3楼 发表于: 2008-07-02
先不说能不能delete
你这样delete 有点不对吧
    delete label;
    if(label != NULL)
    {
        label = NULL;
    }

我觉得应该是
   
    if(label != NULL)
    {
        delete label;
        label = NULL;
    }
离线ldqiang
只看该作者 4楼 发表于: 2008-07-03
同意楼上
离线cn_q_t
只看该作者 5楼 发表于: 2008-07-03
3x 浪漫天使
离线foxyz

只看该作者 6楼 发表于: 2008-07-03
首先,你new出来的都能被自动释放
另外,你也可以自己释放
不过你写的有问题,改成下边的
FindDialog::~FindDialog()
{
    if(label != NULL)
    {
        delete label;
        label = NULL;
    }
下边一样改   
}
离线linshihaoma

只看该作者 7楼 发表于: 2008-07-03
参看我的博客,http://hi.baidu.com/站在水中央
快速回复
限100 字节
 
上一个 下一个