c++ - QtConcurrent 在 reportResultsReady 上崩溃

标签 c++ qt qt5 qt5.3

背景

在我的 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/

相关文章:

Qt5 QML 错误 QtQuick QtGraphicalEffects 未安装

c++ - 使用 mingw 构建 Qt 5.0

c++ - 如何获取clang抽象语法树右侧的整个表达式?

c++ - 为什么 Flex/Lex 会将 C++ 代码的变量重置为其初始值?

c++ - Qt 从内部拉伸(stretch)父窗口?

c++ - Qt 或 libcurl C++ : Facebook api POST request to image upload

c++ - Qt 获取 QWidget 来反射光线

c++ - QFileSystemModel 和 QTreeView - 重置 View 时的奇怪行为

c++ - 如何检测 Asio 库的死锁?

c++ - 关于矩阵计算,是否有 MATLAB 的快速且免费的替代方案?