TechDraw: creates closure for concurrent thread context.

We use a lambda function with a copy of variables
that might be destructed in the original calling thread,
possibly producing dangling references.

See: https://forum.freecad.org/viewtopic.php?t=81260
This commit is contained in:
André Caldas
2023-09-19 02:16:45 -03:00
committed by WandererFan
parent 6a062e95d7
commit 0f27212d57
4 changed files with 28 additions and 34 deletions

View File

@@ -281,11 +281,12 @@ void DrawComplexSection::makeSectionCut(const TopoDS_Shape& baseShape)
connectAlignWatcher =
QObject::connect(&m_alignWatcher, &QFutureWatcherBase::finished, &m_alignWatcher,
[this] { this->onSectionCutFinished(); });
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
m_alignFuture = QtConcurrent::run(this, &DrawComplexSection::makeAlignedPieces, baseShape);
#else
m_alignFuture = QtConcurrent::run(&DrawComplexSection::makeAlignedPieces, this, baseShape);
#endif
// We create a lambda closure to hold a copy of baseShape.
// This is important because this variable might be local to the calling
// function and might get destructed before the parallel processing finishes.
auto lambda = [this, baseShape]{this->makeAlignedPieces(baseShape);};
m_alignFuture = QtConcurrent::run(std::move(lambda));
m_alignWatcher.setFuture(m_alignFuture);
waitingForAlign(true);
}