int main(int argc, char *argv[])
{
QApplication a(argc, argv);
while (true)
{
QObject* pStr = new QObject();
QPointer<QObject> pobj(pStr);
std::thread t4([=]()
{
while (true)
{
if (pobj.isNull())
{
qDebug() << "pobj is null.";
}
else
{
qDebug() << pobj->objectName();//崩溃
return;
}
}
});
t4.detach();
std::thread t3([=]()
{
if (pStr)
{
qDebug() << "pStr is not null.";
}
delete pStr;
});
t3.detach();
}
a.exec();
return 0;
}
QPointer可以监控指针是否已经被销毁,但是多线程使用时还是要注意,该加锁还是要加的,要么大家都在一个线程里面跑也可以。
1088

被折叠的 条评论
为什么被折叠?



