gpt4 book ai didi

c++ - QtConcurrent 在 reportResultsReady 上崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:21:19 31 4
gpt4 key购买 nike

背景

在我的 Qt5.3 应用程序中,我处理了几个耗时的过程(统计计算)。为了能够在一个或多个计算运行时与应用程序一起运行,我创建了名为 ProgressManager 的类。该管理器注册继承自抽象类 IRunnable 并实现纯虚方法 run 的计算对象。

每次启动一个新的耗时操作时,它都会在连接到其进度条的 ProgressManager 中注册,并通过以下函数启动:

void ProgressManager::runProgress(const QVariant &id, const QVariant &param) {

// If progress is not present, exit
if (!progs.contains(id)) {
return;
}

// If progress is not runnable, exit
IRunnable* runnable = dynamic_cast<IRunnable*>(progs.value(id));
if (!runnable) {
return;
}

// Create future watcher
QFutureWatcher<QVariant>* watcher = new QFutureWatcher<QVariant>();
connect(watcher, SIGNAL(finished()), this, SLOT(handleFinished()));

// Register running progress
running.insert(watcher, id);

// Paralelize runnable progress
QFuture<QVariant> future = QtConcurrent::run(runnable, &IRunnable::run, param);
watcher->setFuture(future);
}

并行处理完成后应调用以下函数:

void ProgressManager::handleFinished() {

// Retrieves sender watcher
QObject* s = this->sender();
QFutureWatcher<QVariant>* w = dynamic_cast<QFutureWatcher<QVariant>*>(s);

// Retrieve ID of running progress and delete watcher
QVariant id = running.value(w);
running.remove(w);
delete w;

// Emit progress has finished
emit finished(id);
}

问题

在并行化过程结束之前,一切都在顺利进行。然后,在调用完成信号和 handleFinished 插槽之前,应用程序每次都因段错误而崩溃。

崩溃报告在文件 qfutureinterface.h 的函数 reportResults 的第 211 行,其中函数 reportResultsReady 调用:

194 template <typename T>
195 inline void QFutureInterface<T>::reportResult(const T *result, int index)
196 {
197 QMutexLocker locker(mutex());
198 if (this->queryState(Canceled) || this->queryState(Finished)) {
199 return;
200 }
201
202 QtPrivate::ResultStore<T> &store = resultStore();
203
204
205 if (store.filterMode()) {
206 const int resultCountBefore = store.count();
207 store.addResult(index, result);
208 this->reportResultsReady(resultCountBefore, resultCountBefore + store.count());
209 } else {
210 const int insertIndex = store.addResult(index, result);
211 this->reportResultsReady(insertIndex, insertIndex + 1);
212 }
213 }

最佳答案

我也有类似的问题。目前,我在不使用 QtConcurrent 的情况下使用 QFutureInterface,并且我手动报告 QFuture 已准备就绪。出于某种原因,当从不同的线程调用 QFutureInterface 类的函数时,我时不时会崩溃,这意味着 QFutureInterface 可能不是可重入的,这会自动导致它不是线程安全的,尽管它的代码中有互斥锁等. 无论它是一个导出类,都没有关于它的Qt 文档。我拍了wysota's approach commented here应该查看 QtConcurrent 关于 QFutureInterface 的自定义用法的来源,但如果在 QtConcurrent 使用时崩溃,QFutureInterface 的代码可能有问题。我正在使用 Qt 5.3.2。

关于c++ - QtConcurrent 在 reportResultsReady 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25849652/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com