fix: extend the lifespan of captured objects.

This commit is contained in:
Chris Xiong 2019-11-04 10:56:00 +08:00
parent 2b525c6f3e
commit 3f5bf7c081

View File

@ -235,24 +235,27 @@ QExplicitlySharedDataPointer<DGioFileInfo> DGioFile::createFileInfo(QString attr
unsigned int flagValue = queryInfoFlags; unsigned int flagValue = queryInfoFlags;
FileQueryInfoFlags flags = static_cast<FileQueryInfoFlags>(flagValue); FileQueryInfoFlags flags = static_cast<FileQueryInfoFlags>(flagValue);
Glib::RefPtr<FileInfo> gmmFileInfo; Glib::RefPtr<FileInfo> gmmFileInfo;
QWaitCondition cond; QSharedPointer<QWaitCondition> cond(new QWaitCondition);
QtConcurrent::run([&] { QSharedPointer<QMutex> m(new QMutex);
QtConcurrent::run([&, cond, m, timeout_msec] {
Glib::RefPtr<FileInfo> localret;
QTime t; QTime t;
t.start(); t.start();
try { try {
gmmFileInfo = d->getGmmFileInstance()->query_info(attr.toStdString(), flags); localret = d->getGmmFileInstance()->query_info(attr.toStdString(), flags);
} catch (const Glib::Error &error) { } catch (const Glib::Error &error) {
qDebug() << QString::fromStdString(error.what().raw()); qDebug() << QString::fromStdString(error.what().raw());
return;
} }
if (t.elapsed() < timeout_msec) { if (t.elapsed() < timeout_msec) {
cond.wakeAll(); gmmFileInfo = localret;
cond->wakeAll();
} }
}); });
QMutex m; m->lock();
m.lock(); bool finished = gmmFileInfo || cond->wait(m.data(), timeout_msec);
bool finished = gmmFileInfo || cond.wait(&m, timeout_msec); m->unlock();
m.unlock();
if (finished && gmmFileInfo) { if (finished && gmmFileInfo) {
QExplicitlySharedDataPointer<DGioFileInfo> fileInfoPtr(new DGioFileInfo(gmmFileInfo.release())); QExplicitlySharedDataPointer<DGioFileInfo> fileInfoPtr(new DGioFileInfo(gmmFileInfo.release()));
return fileInfoPtr; return fileInfoPtr;