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 6014817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:44:51+00:00 2026-05-23T02:44:51+00:00

I have a class ‘Downloader’ derived from QObject that runs in a worker thread.

  • 0

I have a class ‘Downloader’ derived from QObject that runs in a worker thread. When the thread is started, the downloader creates a QNetworkAccessManager object on the heap, and starts to request files. I keep track of how many files have been requested and received. Once I’ve gotten all of the files, I delete the QNetworkAccessManager object and exit the thread. My problem is that deleting the manager object causes a crash no matter when or where I do this. I’ve even tried manager->deleteLater(). If I don’t delete it, my code runs great, but I know there is a memory leak there. Here is a stripped down version of my code.

Creating the downloader and thread, and setting signals up so that starting the thread starts the downloads, and when the downloads are complete, the thread stops:

QThread thread;
Downloader downloader;
downloader.setFiles(files);
downloader.moveToThread(&thread);

downloader.connect(&thread, SIGNAL(started()), SLOT(downloadFiles()));
thread.connect(&downloader, SIGNAL(downloadsFinished()), SLOT(quit()));

thread.start();

Implementation for the downloader:

void Downloader::downloadFiles()
{
    QNetworkAccessManager *manager = new QNetworkAccessManager();
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));

    receivedCount = 0;
    requestCount = files.count();

    for (QStringList::const_iterator pos = files.begin(); pos != files.end(); ++pos)
    {
        QUrl url(*pos);
        manager->get(QNetworkRequest(url));
    }
}


void Downloader::finished(QNetworkReply *reply)
{
    // *** Get the file data and process it *** //

    ++receivedCount;
    reply->deleteLater();

    if (receivedCount == requestCount)
    {
//        manager->deleteLater();
        emit downloadsFinished();
    }
}

The commented out line will crash the app. Even deleting the manager in Downloader’s destructor, or setting the downloader as the manager’s parent will crash the app.

I first tried creating the manager as a regular member variable on the stack, but doing so causes errors of it’s own since the manager would be created in the GUI thread and later try to create children on a different thread.

And before anybody says “QNetworkAccessManager is asynchronous. Why use it in a worker thread?” I have my reasons. It shouldn’t be THAT unheard of to do something like this.

  • 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-05-23T02:44:52+00:00Added an answer on May 23, 2026 at 2:44 am

    The only obvious problem is below but I am not sure if you have posted your entire code or not

    void Downloader::downloadFiles()
    {
        QNetworkAccessManager *manager = new QNetworkAccessManager();
    

    You are creating a local QNetworkAccessManager *manager in your method but not keeping a reference to it and then trying to access it within finished() method.

    You should assign new QNetworkAccessManager(); a member variable !

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

Sidebar

Related Questions

I have class method that returns a list of employees that I can iterate
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with a member function that takes a default argument. struct Class
I have class which has a method that needs to return three DataTables. I
I have class that I believe should not be a singleton or static class.
I have Class CustomDate and that is referred in other class called Test. public
I have class dot and have html code like that: <span class=dot>Text1</span> <span class=dot>Text2</span>
I have class Foo which defines property Id . Class Bar inherits from Foo
I have class that predominately consists of gather string input and then subsequently outputting
I have class that is a DataContract and the member values are holding default

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.