线程中的网络请求正在崩溃我的应用程序

时间:2015-01-23 17:52:37

标签: multithreading qt c++11 qthread qtnetwork

目前我的代码崩溃了(SEGFAULT)。

我正在尝试将驱动器中的大量图像与服务器中的计数器部件进行比较。

为了加快这个过程,我从服务器获取图像并比较不同线程上的图像。

从我已经尝试和调试过的,问题是从服务器获取图像(这就是其他调用被注释掉的原因)。

此外,如果我在没有 QtConcurrent :: run 的情况下运行,它不会崩溃,但如果我将信号量 concurrentComparisons 只用一个resorce,它就会崩溃。

最后我还得到以下错误

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()

任何帮助都将非常感谢.....

相关代码:

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;
}

1 个答案:

答案 0 :(得分:0)

不幸的是,这与代码无关。

其中一张图片已损坏,并且在比较中是分段的。