Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Memory error on QThread::exec()
Forum Updated to NodeBB v4.3 + New Features

Memory error on QThread::exec()

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    serpulga
    wrote on last edited by
    #1

    Hi,

    I'm writing a Qt application that produces a number of threads, each thread
    has the task to download an image file from the internet.

    @
    ImageDownload::ImageDownload(const QUrl & url_, QObject * parent) :
    QThread(parent)
    {
    url = url_;
    }

    void ImageDownload::run()
    {
    QNetworkRequest imageRequest(url);
    QNetworkAccessManager network;

    network.get(imageRequest);
    
    connect(&network, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
    
    exec();
    

    }
    @

    The code that creates the QThread objects looks like this:

    @
    ImageDownload * download = new ImageDownload(nextImage);

        connect(download, SIGNAL(imageReady(QUrl,bool,ImageDownload*)),
                this, SLOT(imageReady(QUrl,bool,ImageDownload*)), Qt::QueuedConnection);
    
        download->start();
    

    @

    The problem is that the application is unstable, as every now and then it exits with the following error:

    @
    ImageShow(15175,0x10a64a000) malloc: *** error for object 0x10301efc0: pointer being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug
    @

    ImageShow is the name of my Qt project.
    When debugged the application, I found out that when application always breaks, it does it
    at the 'exec()' line, in the class constructor.

    I wonder if someone can help me out here, maybe there is something
    that I'm doing wrong?

    Thanks,

    Sergio

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mohsen
      wrote on last edited by
      #2

      what's the mean for using exec() on run()? you can't call exec() to thread inside itself.
      it's not the way you block the application until the progress finish.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        issam
        wrote on last edited by
        #3

        bq. what’s the mean for using exec() on run() ?

        No problem with exec(), signals and slots need an event loop to be executed, and this is what it does QThread::exec().

        http://www.iissam.com/

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          First of all: you don't need threads for this. The network API's in Qt are already asynchronous. Then, you are using threads in the wrong way. Don't subclass QThread if you use signals and slots. It almost never is what you meant. Instead, create a QObject derived worker object and use that with a vanilla QThread instance.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MichelS
            wrote on last edited by
            #5

            Is it possible that you emit your @imageReady(QUrl,bool,ImageDownload*)@
            Signal twice?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              serpulga
              wrote on last edited by
              #6

              Hi, thanks for the answers.

              [quote author="Andre" date="1348219572"]First of all: you don't need threads for this. The network API's in Qt are already asynchronous. Then, you are using threads in the wrong way. Don't subclass QThread if you use signals and slots. It almost never is what you meant. Instead, create a QObject derived worker object and use that with a vanilla QThread instance. [/quote]

              Do you think just instantiating QNetworkAccessManager objects in some sort of loop will work for me?
              I want a couple of hundreds of images downloaded as quick as possible.
              Won't a thread solution give me at least a speed advantage?

              [quote author="MichelS" date="1348219682"]Is it possible that you emit your @imageReady(QUrl,bool,ImageDownload*)@
              Signal twice?[/quote]

              This signal is only emitted once inside the 'downloadFinished(QNetworkReply*)' slot.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                [quote author="serpulga" date="1348248152"]
                Do you think just instantiating QNetworkAccessManager objects in some sort of loop will work for me?
                I want a couple of hundreds of images downloaded as quick as possible.
                Won't a thread solution give me at least a speed advantage?[/quote]

                No, not really. You're dealing with two limited (and shared) resources: your network access and your HD access. Using multiple threads will make better use of your CPU, but that is most likely not the bottleneck here. Also, you need only a single QNetworkAccessManager, which you can feed multiple requests simultaniously. I would not feed it hundreds of requests at the same time though. I think it will will process 6 or so requests at a time.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  serpulga
                  wrote on last edited by
                  #8

                  [quote author="Andre" date="1348262465"]
                  [quote author="serpulga" date="1348248152"]
                  Do you think just instantiating QNetworkAccessManager objects in some sort of loop will work for me?
                  I want a couple of hundreds of images downloaded as quick as possible.
                  Won't a thread solution give me at least a speed advantage?[/quote]

                  No, not really. You're dealing with two limited (and shared) resources: your network access and your HD access. Using multiple threads will make better use of your CPU, but that is most likely not the bottleneck here. Also, you need only a single QNetworkAccessManager, which you can feed multiple requests simultaniously. I would not feed it hundreds of requests at the same time though. I think it will will process 6 or so requests at a time.[/quote]

                  You make a great point here.
                  I did read about the '6' requests limit before. I think I will change
                  my implementation to use a QNetworkAccessManager class.

                  Although, I'm still curious about the error I'm getting.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved