Search code examples
qtaccess-violationqnetworkaccessmanagerqt5.5

What is the correct way to "stop / shutdown" a QNetworkAccessManager?


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

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

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.


Solution

  • 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.

    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) It is understood that requests are performed asynchronously - we have good design reasons for the threaded worker.