我正在尝试使用单独的线程下载带有QNetworkAccessManager :: get()的网页,当我运行该程序时,它会随机崩溃(可能是一小时或两小时)。代码示例:
class SpiderThread : public QThread
{
Q_OBJECT
public:
SpiderThread();
void run();
private:
bool _stopped;
};
void SpiderThread::run()
{
while (!_stopped) {
DownloadManager downloadManager();
QUrl u = getOneUrl();
QString content = downloadManager.getContent(u);
// some other code
}
}
QString DownloadManager::getContent(const QUrl &url)
{
QEventLoop loop;
QNetworkAccessManager manager;
QNetworkRequest request(url);
request.setRawHeader("User-Agent", _userAgent.toAscii());
QNetworkReply *reply = manager.get(request);
connect(reply, SIGNAL(finished()),
SLOT(replyFinished()));
connect(reply, SIGNAL(finished()),
&loop, SLOT(quit()));
loop.exec();
if (reply->error() != QNetworkReply::NoError) {
if (reply->isRunning())
reply->abort();
reply->deleteLater();
return QString();
}
int httpCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (httpCode != 200)
return QString();
QByteArray data = reply->readAll();
reply->deleteLater();
return QString(data);
}
我正在使用QEventloop来阻止http请求直到它完成,当我运行这些代码时,它总是崩溃并显示错误消息:
pure virtual method called terminate called without an active exception
gdb调试信息:
(gdb) bt #0 0x0000003c0dc30285 in raise () from /lib64/libc.so.6 #1 0x0000003c0dc31d30 in abort () from /lib64/libc.so.6 #2 0x0000003d992bed94 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib64/libstdc++.so.6 #3 0x0000003d992bce46 in ?? () from /usr/lib64/libstdc++.so.6 #4 0x0000003d992bce73 in std::terminate() () from /usr/lib64/libstdc++.so.6 #5 0x0000003d992bd3cf in __cxa_pure_virtual () from /usr/lib64/libstdc++.so.6 #6 0x00002aaaabc3cc35 in QCoreApplication::postEvent(QObject*, QEvent*, int) () from /usr/local/Trolltech/Qt-4.8.2/lib/libQtCore.so.4 #7 0x00002aaaabc5301d in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) () from /usr/local/Trolltech/Qt-4.8.2/lib/libQtCore.so.4 #8 0x00002aaaab7b81a1 in ?? () from /usr/local/Trolltech/Qt-4.8.2/lib/libQtNetwork.so.4 #9 0x00002aaaab7ac3ad in ?? () from /usr/local/Trolltech/Qt-4.8.2/lib/libQtNetwork.so.4 #10 0x00002aaaab7c8d1d in ?? () from /usr/local/Trolltech/Qt-4.8.2/lib/libQtNetwork.so.4 #11 0x00002aaaab7cadb0 in ?? () from /usr/local/Trolltech/Qt-4.8.2/lib/libQtNetwork.so.4 #12 0x00002aaaab7a89ce in QNetworkAccessManager::createRequest(QNetworkAccessManager::Operation, QNetworkRequest const&, QIODevice*) () from /usr/local/Trolltech/Qt-4.8.2/lib/libQtNetwork.so.4 #13 0x00002aaaab7a5ea4 in QNetworkAccessManager::get(QNetworkRequest const&) () from /usr/local/Trolltech/Qt-4.8.2/lib/libQtNetwork.so.4 #14 0x00000000004088fe in DownloadManager::getContent (this=0x409fff40, url=..., toCodec=...) at src/downloadmanager.cpp:55 #15 0x0000000000415248 in SpiderThread::run (this=0x7fffffffe2b0) at src/spiderthread.cpp:27 #16 0x00002aaaabb2c3b9 in ?? () from /usr/local/Trolltech/Qt-4.8.2/lib/libQtCore.so.4 #17 0x0000003c0e40677d in start_thread () from /lib64/libpthread.so.0 #18 0x0000003c0dcd325d in clone () from /lib64/libc.so.6
请帮助我,我花了很多时间找出错误,但它仍然无效。我犯了一些错误?
答案 0 :(得分:2)
你有不必要的复杂事情。 QThread::run()
的默认实现已经创建并运行了一个事件循环。不要从QThread派生。将您的功能放入QObject,并在启动线程后调用该对象上的moveToThread(thread)
,如有必要!使用信号和插槽完成任务。 QNetworkAccessManager已经使用了工作线程IIRC,因此在另一个专用线程中创建它可能毫无意义。