Memory error on QThread::exec()
-
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
-
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. -
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().
-
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.
-
Is it possible that you emit your @imageReady(QUrl,bool,ImageDownload*)@
Signal twice? -
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.
-
[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 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.