Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:49:59+00:00 2026-06-05T06:49:59+00:00

I have a Qt application that launches two threads from the main thread at

  • 0

I have a Qt application that launches two threads from the main thread at start up. Both these threads make network requests using distinct instances of the QNetworkAccessManager object. My program keeps crashing about 50% of the times and I’m not sure which thread is crashing.

There is no data sharing or signalling occurring directly between the two threads. When a certain event occurs, one the threads signals the main thread, which may in turn signal the second thread. However, by printing logs, I am pretty certain the crash doesn’t occur during the signalling.

The structure of both threads is as follows. There’s hardly any difference between the threads except for the URL etc.

MyThread() : QThread() {
    moveToThread(this);
}

MyThread()::~MyThread() {
    delete m_manager;
    delete m_request;
}

MyThread::run() {
    m_manager = new QNetworkAccessManager();
    m_request = new QNetworkRequest(QUrl("..."));

    makeRequest();
    exec();
}

MyThread::makeRequest() {
    m_reply = m_manager->get(*m_request);
    connect(m_reply, SIGNAL(finished()), this, SLOT(processReply()));
    // my log line
}

MyThread::processReply() {
    if (!m_reply->error()) {
        QString data = QString(m_reply->readAll());
        emit signalToMainThread(data);
    }
    m_reply->deleteLater();
    exit(0);
}

Now the weird thing is that if I don’t start one of the threads, the program runs fine, or at least doesn’t crash in around 20 invocations. If both threads run one after the other, the program doesn’t crash. The program only crashes about half the times if I start and run both the threads concurrently.

Another interesting thing I gathered from logs is that whenever the program crashes, the line labelled with the comment my log line is the last to be executed by both the threads. So I don’t know which thread causes the crash. But it leads me to suspect that QNetworkAccessManager is somehow to blame.

I’m pretty blank about what’s causing the crash. I will appreciate any suggestions or pointers. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:50:00+00:00Added an answer on June 5, 2026 at 6:50 am

    First of all you’re doing it wrong! Fix your threading first

    // EDIT
    From my own experience with this pattern i know that it may lead to many unclear crashes. I would start from clearing this thing out, as it may straighten some things and make finding problem clear. Also I don’t know how do you invoke makeRequest. Also about QNetworkRequest. It is only a data structure so you don’t need to make it on heap. Stack construction would be enough. Also you should remember (or protect somehow) from overwriting m_reply pointer. Do you call makeRequest more than once? If you do, then it may lead to deleting currently processed request after previous request finished.

    What does happen if you call makeRequest twice:

    1. First call of makeRequest assigns m_reply pointer.
    2. Second call of makeRequest assigns m_reply pointer second time (replacing assigned pointer but not deleting pointed object)
    3. Second request finishes before first, so processReply is called. deleteLater is queued at second
    4. Somewhere in eventloop second reply is deleted, so from now m_reply pointer is pointing at some random (deleted) memory.
    5. First reply finishes, so another processReply is called, but it operates on m_reply that is pointing a garbage, so every call at m_reply produces crash.

    It is one of possible scenarios. That’s why you don’t get crash every time.

    I’m not sure why do you call exit(0) at reply finish. It’s also incorrect here if you use more then one call of makeRequest. Remember that QThread is interface to a single thread, not thread pool. So you can’t call start() second time on thread instance when it is still running. Also if you’re creating network access manager in entry point run() you should delete it in same place after exec(). Remember that exec() is blocking, so your objects won’t be deleted before your thread exits.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that fires 2 threads, the 1st launches another class to
I have an application that contains two Activities with <intent-filter> <action android:name=android.intent.action.MAIN/> <category android:name=android.intent.category.LAUNCHER/>
I have a MFC application that launches a IWebBrowser2 window. On users computers where
I have an application that is launched from within a service using a local
I have two application that need to talk to each other. App1 needs to
So basically, I have two applications. Application 1 launches Application 2 but it remains
I have a UITableView with two sections, which are both empty when my application
here's my problem: I have an application that launches a simulated server locally. The
Our application launches ListActivity that displays two menu options with launch a football game
I am doing the following things to make sure that my application launches only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.