【问题标题】:Network request in threads is crashing my application线程中的网络请求使我的应用程序崩溃
【发布时间】:2015-01-23 17:52:37
【问题描述】:

目前,我有一个崩溃的代码 (SEGFAULT)。

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

为了加快进程,我从服务器获取图像并在不同线程上比较图像。

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

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

最后我也得到以下错误

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

【问题讨论】:

  • 并不是所有相关代码。 connect() 在哪里调用 QNetworkConfigurationManager 对象槽?指向应该发出信号的对象的指针设置为 null。
  • 我没有在我的代码中使用 QNetworkConfigurationManager。它在 QNetworkAccessManager 内部使用。

标签: multithreading qt c++11 qthread qtnetwork


【解决方案1】:

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

其中一张图片已损坏,并且在比较中出现了段错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-22
    • 2015-08-11
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    相关资源
    最近更新 更多