Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
marcel808
Posts: 27
Registered: ‎09-03-2012
My Device: Blackberry Playbook
My Carrier: None

QThreads not closing correctly

I've followed the recommended use of QThread (http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/The-Recommended-Way-to-Use-QTh...

My app is trying to run threads, each thread runs an http request. I found I was receiving crashes after about 120 threads had been run so I created the above much simplified example to try and pinpoint my error.

 

I've created the below simple example that is (I think) failing to cleanly remove the old threads or worker instances, finally crashing.

 

My main app constructor creates a thread; this thread just contains an infinite loop chucking out a signal every 10th of a second, the slot that catches this signal in also in my main app. This bit is very simple, not the problem (I think).

Proj2::Proj2(bb::cascades::Application *app)
: QObject(app)
{
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    root = qml->createRootObject<AbstractPane>();
    app->setScene(root);

    QThread* thread = new QThread;
    MainThread* worker = new MainThread();
    worker->moveToThread(thread);
    // connect thread started() to the process slot in the worker
    connect(thread, SIGNAL(started()), worker, SLOT(process()));
    connect(worker, SIGNAL(responded()), this, SLOT(myslot()));
    thread->start();
}

 


So the slot in my main app gets called 10 times a second and contains the code:

void Proj2::myslot()
{
    QThread* thrd = new QThread;
    MyWorker* worker = new MyWorker();
    worker->moveToThread(thrd);
    connect(thrd, SIGNAL(started()), worker, SLOT(process()));
    connect(worker, SIGNAL(finished()), thrd, SLOT(quit()));
    connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    connect(thrd, SIGNAL(finished()), thrd, SLOT(deleteLater()));
    thrd->start();
}

 

 

And the MyWorker class is almost identical to the code in knoledge base example. When it starts, it calls process() which is just:

#include "MyWorker.h"
#include <stdio.h>
#include <unistd.h>

MyWorker::MyWorker() {
}

MyWorker::~MyWorker() {
}

void MyWorker::process()
{
    emit finished();
}

 

If I run it, after 10 seconds or so I get the error:

QEventDispatcherUNIXPrivate(): Unable to create thread pipe
Too many open files

 

If anyone could advice I'd really appreciate it!

 

 

Please use plain text.
Contributor
marcel808
Posts: 27
Registered: ‎09-03-2012
My Device: Blackberry Playbook
My Carrier: None

Re: QThreads not closing correctly

anyone? :smileysad:

Please use plain text.
New Developer
vladest
Posts: 88
Registered: ‎06-16-2012
My Device: N950/E7/N808/N9/X7/BB10 Alpha
My Carrier: Kievstar

Re: QThreads not closing correctly

got similar issue with the example

 

resoved by:

1. disable

 

connect(worker, SIGNAL(finished()), thread, SLOT(quit()));

 2. added to the worker finished() slot:

thread->quit();
thread->wait();

 

Qt/Symbian/Meego/BB10/Cascades developer
Please use plain text.
Contributor
marcel808
Posts: 27
Registered: ‎09-03-2012
My Device: Blackberry Playbook
My Carrier: None

Re: QThreads not closing correctly

hi vladest

many thanks for your help ,really appreciate it.

 

Step 2 was to add 

thread->quit();
thread->wait();

 I didn't fully understand...right now, in my worker thread, in Process() I emit a finished signal. I don't know about the thread itself in the worker class.

Should I pass a pointer to the thread into the worker class and quite/wait in the worker class? (instead of emitting a finished signal)

Or should I emit a finished signal and in my main class catch the signal and somehow get the thread info?

 

Apologies for my ignorance

 

 

Please use plain text.
BlackBerry Development Advisor (Retired)
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10
My Carrier: Vodafone

Re: QThreads not closing correctly

For your particular requirements, I wonder if QThreadPool might be a better fit?

 

http://doc.qt.digia.com/4.6/qthreadpool.html

 

 

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Please use plain text.
New Developer
vladest
Posts: 88
Registered: ‎06-16-2012
My Device: N950/E7/N808/N9/X7/BB10 Alpha
My Carrier: Kievstar

Re: QThreads not closing correctly

yes, you right

finished() slot in main class, having both worker and thread pointers

Qt/Symbian/Meego/BB10/Cascades developer
Please use plain text.
Contributor
marcel808
Posts: 27
Registered: ‎09-03-2012
My Device: Blackberry Playbook
My Carrier: None

Re: QThreads not closing correctly

hi vladest

nearly there :smileyhappy: again, huge thanks.

 

When I connect signal and slot, can I pass the thread here?

Something like:

void Proj2::threadfinished(QThread *thread)
{
	printf("threadfinished\n");
	fflush(stdout);
	thread->quit();
	thread->wait();
}

 

    QThread* thrd = new QThread;
    MyWorker* worker = new MyWorker();
    worker->moveToThread(thrd);
....
    connect(worker, SIGNAL(finished()), this, SLOT(threadfinished(thrd)));

 It compiles fine but the slot isn't called. If I stop trying to pass the thread across, it works. 

I'm unsure how to pass the thread info to the slot

 

thanks

 

Please use plain text.
New Developer
vladest
Posts: 88
Registered: ‎06-16-2012
My Device: N950/E7/N808/N9/X7/BB10 Alpha
My Carrier: Kievstar

Re: QThreads not closing correctly

hi

 

it doesnt makes sense to pass thread to the slot cause this (ie main class pointer) _already_ has the thread pointer

Qt/Symbian/Meego/BB10/Cascades developer
Please use plain text.