dimanche 25 janvier 2015

Network request in threads is crashing my application

Currently I have a code that is crashing (SEGFAULT) on me.


I am trying to compare a big amount of images that are in my drive to their counter parts in a server.


To speed up the process I get the image from the server and compare the images on a different thread.


From what I already tried and debugged, the issue is in getting the image from the server (that is why the other calls are commented out).


Also if I run without the QtConcurrent::run it does not crash, but if I put semaphore concurrentComparisons with only one resorce, it will crash.


Finally I also get the following errors



QObject::connect: Cannot connect (null)::configurationAdded(QNetworkConfiguration) to QNetworkConfigurationManager::configurationAdded(QNetworkConfiguration)
QObject::connect: Cannot connect (null)::configurationRemoved(QNetworkConfiguration) to QNetworkConfigurationManager::configurationRemoved(QNetworkConfiguration)
QObject::connect: Cannot connect (null)::configurationChanged(QNetworkConfiguration) to QNetworkConfigurationManager::configurationChanged(QNetworkConfiguration)
QObject::connect: Cannot connect (null)::onlineStateChanged(bool) to QNetworkConfigurationManager::onlineStateChanged(bool)
QObject::connect: Cannot connect (null)::configurationUpdateComplete() to QNetworkConfigurationManager::updateCompleted()


Any help would be very appreciated.....


Relevant code:



QSemaphore FileComparisonInfo::concurrentComparisons(1);





QtConcurrent::run( [this, localPath, imageURL]()
{
ImageComparer cmp;

FileComparisonInfo::concurrentComparisons.acquire();

//cmp.setImageLeftPath(localPath);
cmp.setImageRightPath(imageURL);
//cmp.createDifferenceImage();

FileComparisonInfo::concurrentComparisons.release();
});





void ImageComparer::setImageRightPath(QString path)
{
this->rightImagePath = path;
this->imageRight = getImage(path);
}





QImage* ImageComparer::getImage(QString path)
{
QUrl url(path);
QFile file(path);

if(file.exists())
{
return new QImage(path);
}
else if(url.isValid())
{
return getImageFromURL(path);
}
}





QImage* ImageComparer::getImageFromURL(QString url)
{

QNetworkAccessManager * tempNAM = new QNetworkAccessManager();
QNetworkReply *imageConnection = tempNAM->get( QNetworkRequest( QUrl( url ) ));

QEventLoop loop;
connect(imageConnection, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();

QImage * downloadedImage;
if(imageConnection->error() != QNetworkReply::NoError)
{
qDebug() << imageConnection->errorString();
downloadedImage = new QImage();
}
else
{
QByteArray data = imageConnection->readAll();
downloadedImage = new QImage(QImage::fromData(data));
}

tempNAM->deleteLater();
imageConnection->deleteLater();

return downloadedImage;
}

Aucun commentaire:

Enregistrer un commentaire