I am writing a program that uses QtConcurrent to start threads. In my case, I use it to render a QGraphicsView when I use the mouse scroll.
I am starting the threads using the following code:
if (future.isRunning()) {
future.cancel();
}
future = QtConcurrent::run(this,&AeVectorLayer::render, renderparams, pos);
watcher.setFuture(future);
When the thread is finished I catch the signal finished with a QfutureWatcher.
This is my render function:
QList<AeGraphicsItem*> AeVectorLayer::render(Ae::renderParams renderparams, int pos)
{
AeVectorHandler *coso = new AeVectorHandler();
coso->openDataset(AeLayer::absoluteFilePath);
coso->setTransformCoordinates(myEPSG);
QList<AeGraphicsItem*> bla = coso->getItems(renderparams.sceneRect.x(),
renderparams.sceneRect.y(), renderparams.sceneRect.width(),
renderparams.sceneRect.height(), renderparams.zoom, color, this);
for (int i = 0; i < bla.size(); i++)
bla.at(i)->setZValue((qreal)pos);
delete coso;
return bla;
}
As you can see, I have a QList<QGraphicsItem*> in my render function. How can I destroy this list when the future is cancelled? I undestand that in my code I am redefining the future variable but I do not know how to avoid it.
Stop trying to manually manage memory and instead use a smart pointer that fits your use case. Because you use move-unaware
QFuture, you will need anstd::shared_ptr. Once theQFuture/QFutureWatchers go out of scope, and you hold no moreshared_ptrinstances, the resource will be deleted. So in your case yourrenderfunction should return aQList<std::shared_ptr<AeGraphicsItem>>. Be careful when you transfer ownership from theshared_ptrs to e.g. aQGraphicsScene: you mustreleasefrom theshared_ptron ownership transfer.Note that your
isRunningcheck followed bycancelis fundamentally flawed: the future could be running when you callisRunningbut be finished by the time you callcancel. If you want to cancel it, just callcancel. Also note that you cannot meaningfully cancelQFutures returned byQtConcurrent::runso what you're doing is very very wrong in and of itself.