【发布时间】:2014-04-25 12:34:59
【问题描述】:
我有一个从QCoreApplication 类派生的应用程序,并且有一个子线程成员。当我删除应用程序对象时,它有时会删除,有时不会。
class My_class :public QCoreApplication{
private:
My_object* obj;
QThread *th;
public:
My_class(){
obj=new My_object();
th= new QThread();
obj->movetoThread(th); // so it starts to run
}
~My_class(){
delete obj;
cout<<" App Destructor called"<<endl;
}
static void exit_(){quit();}
};
// So in main I suddenly close my application and i want to exit and delete obj;
int main()
{
My_class app;
signal(SIGTERM,&app.exit_);
signal(SIGINT,&app.exit_);
signal(SIGBREAK,&app.exit_);
return app.exec();
}
// The obj destructor is;
~My_object::My_object(){cout<<"Object dest called"<<endl;}
// The output of my program always writes "Object dest called"
//But sometimes writes " App Destructor called".
所以我的程序总是进入 obj 的 destructpr,但有时它会进入 app 析构函数,有时不会。如何实现?
【问题讨论】:
-
我认为它确实指出了发布完整、独立、可编译的代码是多么非常重要。这样我们就不必在这样的愚蠢错误上浪费时间了。
-
@KubaOber 是的,你是对的