本文介绍了“停止/关机"的正确方法是什么?QNetworkAccessManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QNetworkAccessManager .假设我有一个待处理的请求:

I have a QNetworkAccessManager . Let's assume I have a pending request:

QNetworkRequest request(url);
this->m_networkManager->get(request)

我可以随时关闭 QNetworkAccessManager 吗?我之所以这么问是因为当我在挂起的 get 请求期间销毁我的对象 m_networkManager 时,我看到了写入访问冲突.

Can I shutdown the QNetworkAccessManager at any time? I am asking because I see a write access violation when I destruct my object m_networkManager during a pending get request.

或者我怎样才能安全地销毁管理器,似乎没有停止或关闭功能.

Or how can I safely destroy the manager, there seems to be no stop or shutdown functionality.

推荐答案

根本原因是我们的 QNetworkAccessManager 用于线程工作者 (1).所以很明显,当 QNetworkAccessManager 被删除,并且在这一步(并且只有那时)清理挂起的 QNetworkReply 对象时,问题就出现了.

The root cause is that our QNetworkAccessManager is used in a threaded worker (1). So obviously when the QNetworkAccessManager is deleted, and in this step (and only then) cleans up pending QNetworkReply objects, the problem arises.

分析:没有待处理的回复,或者在主线程中使用时,在同一场景中没有问题.如果在将 QNetworkAccessManager 移回主线程之前将其删除,则可以避免该问题.结论(或推测):当 QNetworkAccessManager 尝试删除在不同线程中创建的 QNetworkReply 时会出现问题.

Analysis:With no pending replies, or when used in the main thread, there is no issue in the same scenario. The issue can be avoided if the QNetworkAccessManager is deleted just before it is moved back to the main thread. Conclusion (or speculation): The issue occurs when the QNetworkAccessManager tries to delete a QNetworkReply created in a different thread.

(1) 据了解,请求是异步执行的——我们有很好的设计理由来设计线程工作者.

(1) It is understood that requests are performed asynchronously - we have good design reasons for the threaded worker.

这篇关于“停止/关机"的正确方法是什么?QNetworkAccessManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 18:35