|
5Qter豆
本帖最后由 caiwei_cs 于 2016-12-16 11:09 编辑
程序如下:
main:
C/C++ code?
1
2
3
4
5
6
7
8
9
10
11
12
13
| #include <QCoreApplication>
#include "cc.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// cc *k = new cc;//不会报错
cc k;//会报错
return a.exec();
}
|
cc.cpp
C/C++ code?
1
2
3
4
5
6
7
8
9
| #include "cc.h"
#include <QTimer>
#include <QDebug>
cc::cc(QObject *parent) : QObject(parent)
{
this->deleteLater();
qDebug()<<"deletelater called";
}
|
cc.h
C/C++ code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| #ifndef CC_H
#define CC_H
#include <QObject>
class cc : public QObject
{
Q_OBJECT
public:
explicit cc(QObject *parent = 0);
signals:
public slots:
};
#endif // CC_H
|
运行程序会报错:deletelater called
*** Error in `/home/caiwei/tmp/11/build-dd-Qt_5_2_0_X86-Debug/dd': double free or corruption (out): 0x00007fff4779b910 ***
按 <RETURN> 来关闭窗口...
如果使用cc *k = new cc;就不会报错,或者不是在main函数里面使用就不会。
想不通为什么?
有人说,是main函数结束会析构一次,我自己deletelater一次,所以会有多重释放。但是我的理解是,return a.exec();是不会退出的。求助。 |
最佳答案
查看完整内容
因为cc K等价于cc *K = new cc; delete K;
而不是单纯等价于cc *K = new cc;
|