Threads problem



  • Hi,
    I'm trying to run a thread with some opencv code to detect faces on my program, and I just can't run it and i got no error.

    in the meantime i've tried to do a while( i < 1000000) i++ prinft(i) to test the threads and i've noticed that if I use a thread my printf stops at +- 17000 and if I run it with no thread it runs until the 1000000. please tell me what is wrong? is QT limiting memory in threads?

    code:
    @
    faceTracker::faceTracker()
    {
    i=0;

    }

    void faceTracker::run( )
    {
    while(i < 1000000){
    i++;
    printf("%d \n", i);

    }
    

    }
    @

    main:
    @
    int main(int argc, char *argv[])
    {

    faceTracker * ft = new faceTracker;
    ft->run(); // This is the version with no thread, to have thread just put it ft->start();
    
    
    QApplication app(argc, argv);
    

    }
    @



  • Your problem is the main functions:

    @
    int main(int argc, char *argv[])
    {

    faceTracker * ft = new faceTracker;
    ft->run(); // This is the version with no thread, to have thread just put it ft->start();
    QApplication app(argc, argv); // <-- this is no wait!
    

    }
    @

    you start your thread and then exit main. exiting main is exiting your application which means, th eprocess is exited and all threads are killed.

    Try the following code:

    @
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv); // <-- this is no wait!

    faceTracker * ft = new faceTracker;
    ft->start();
    ft->wait();
    

    }
    @



  • Forgive my ignorance here but this is an opportunity to learn something about Qt. I only write GUI applications and usually there is an application.show() which starts an event loop.

    Does the above example cover non-GUI applications?



  • QCoreApplication. That's all.



  • [quote author="Gerolf" date="1302635887"]Your problem is the main functions:

    @
    int main(int argc, char *argv[])
    {

    faceTracker * ft = new faceTracker;
    ft->run(); // This is the version with no thread, to have thread just put it ft->start();
    QApplication app(argc, argv); // <-- this is no wait!
    

    }
    @

    you start your thread and then exit main. exiting main is exiting your application which means, th eprocess is exited and all threads are killed.

    Try the following code:

    @
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv); // <-- this is no wait!

    faceTracker * ft = new faceTracker;
    ft->start();
    ft->wait();
    

    }
    @
    [/quote]
    thanks a lot Gerolf! that solved my problem! I just forgot that the wait is also to make the main thread wait for the other, i was thinking that ft->wait() will put ft on hold

    can you explain me why now this app with no wait works good?

    code:
    @
    #include "thread.h"
    #include <cassert>
    #include <iostream>
    #include <QHash>
    #include <QDir>
    #include <QDebug>
    #include <QThread>

    thread::thread(){

    }

    void thread::run(){
    int i = 0;

    while(i < 1000000){
        printf("%d \n", i);
        i++;
    }
    

    }
    @

    main:
    @
    #include <QtGui/QApplication>
    #include "thread.h"
    #include <QList>
    #include <QDebug>
    #include <QObject>
    #include <QWidget>
    #include <QGraphicsObject>
    #include <QState>
    #include <QGraphicsObject>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    thread * ft = new thread;
    ft->start();
    
    
    return a.exec&#40;&#41;;
    

    }
    @

    as far as I see the apps are the same :o



  • Now I'm trying use a thread on the openCV and my program is closing with no specific error in the crash report i have this:
    @

    Thread 7 Crashed:
    0 OpenCV 0x0001209a GOMP_parallel_end + 58
    1 OpenCV 0x00012121 GOMP_parallel_start + 17
    2 OpenCV 0x00335108 cvSetImagesForHaarClassifierCascade + 1560
    3 ??? 0x0000002f 0 + 47
    @

    my code:
    @
    faceTracker::faceTracker()
    {
    qDebug("teste1");
    filename = "/haarcascades/haarcascade_frontalface_alt_tree.xml";

    /* load the classifier
        note that I put the file in the same directory with this code */
    cascade = ( CvHaarClassifierCascade* )cvLoad( filename, 0, 0, 0 );
    if(!cascade){
        qDebug("cascade!");
        exit(0);
    }
    /* setup memory buffer; needed by the face detector */
    storage = cvCreateMemStorage( 0 );
    if(!storage){
        qDebug("storage");
        exit(0);
    }
    
    /* initialize camera */
    capture = cvCaptureFromCAM( 0 );
    if(!capture){
        qDebug("capture");
        exit(0);
    }
    
    /* always check */
    assert( cascade && storage && capture );
    

    }

    void faceTracker::detectFaces( IplImage img )
    {
    /
    detect faces */
    faces = cvHaarDetectObjects(
    img,
    cascade,
    storage,
    1.1,
    3,
    0 /CV_HAAR_DO_CANNY_PRUNNING/,
    cvSize( 40, 40 ) );

    /* for each face found, draw a red box */
    for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
        CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
        cvRectangle( img,
            cvPoint( r->x, r->y ),
            cvPoint( r->x + r->width, r->y + r->height ),
            CV_RGB( 255, 0, 0 ), 1, 8, 0 );
        qDebug("caras: %d", faces->total);
    }
    
    /* display video */
    cvShowImage( "video", img );
    

    }

    void faceTracker::run()
    {
    qDebug("teste2");

    while( key != 'q' ) {
        /* get a frame */
        frame = cvQueryFrame( capture );
        /* always check */
        if( !frame ) break;
    
        /* 'fix' frame */
        cvFlip( frame, frame, 1 );
    
        frame->origin = 0;
    
        /* detect faces and display video */
        detectFaces( frame );
    
    
        /* quit if user press 'q' */
        key = cvWaitKey( 10 );
    
    }
    
    /* free memory */
    cvReleaseCapture( &capture );
    cvDestroyWindow( "video" );
    cvReleaseHaarClassifierCascade( &cascade );
    cvReleaseMemStorage( &storage );
    

    }

    @

    main:
    @
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    app.setApplicationName( "DisplUM" );
    
    faceTracker * ft = new faceTracker;
    ft->start();
    ft->wait();
    

    }
    @



  • [quote author="MAMartins" date="1302640707"]
    main:
    @
    #include <QtGui/QApplication>
    #include "thread.h"
    #include <QList>
    #include <QDebug>
    #include <QObject>
    #include <QWidget>
    #include <QGraphicsObject>
    #include <QState>
    #include <QGraphicsObject>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    thread * ft = new thread;
    ft->start();
    
    
    return a.exec&#40;&#41;;
    

    }
    @

    as far as I see the apps are the same :o
    [/quote]

    The difference lies in your a.exec().
    In the previous one, you were creating the app on stack then exiting the main funciton, thus exiting program.
    In your second one, you tell the application to start the main thread, leaving time to switch to other threads.

    Most probably,your first application exited directly, without doing anything, while this one waits for you to close the window (or at least, doesn't end itself when processing is over).



  • [quote author="MAMartins" date="1302656456"]Now I'm trying use a thread on the openCV and my program is closing with no specific error in the crash report i have this:
    @

    Thread 7 Crashed:
    0 OpenCV 0x0001209a GOMP_parallel_end + 58
    1 OpenCV 0x00012121 GOMP_parallel_start + 17
    2 OpenCV 0x00335108 cvSetImagesForHaarClassifierCascade + 1560
    3 ??? 0x0000002f 0 + 47
    @
    [/quote]

    I'm not 100% sure, but it smells like NULL pointer dereferencement (0+47 : 0 is base pointer, 47 is offset, sum of them equals 02f, which is invalid address and crashed your program). Now, I definitely lack code to tell more.



  • [quote author="florent.revelut" date="1302673342"][quote author="MAMartins" date="1302640707"]
    main:
    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    thread * ft = new thread;
    ft->start();
    
    
    return a.exec&#40;&#41;;
    

    }
    @

    as far as I see the apps are the same :o
    [/quote]

    The difference lies in your a.exec().
    In the previous one, you were creating the app on stack then exiting the main funciton, thus exiting program.
    In your second one, you tell the application to start the main thread, leaving time to switch to other threads.

    Most probably,your first application exited directly, without doing anything, while this one waits for you to close the window (or at least, doesn't end itself when processing is over).[/quote]

    a.exec() does not start the main thread, The main thread is started by the OS. a.exec() starts the eventLoop which is used to get UI and other events and deliver them to the corresponding objects. It ius running untill the loop is exited, e.g. by closing all top level windows, calling QApplication::quit, etc...



  • [quote author="florent.revelut" date="1302673593"][quote author="MAMartins" date="1302656456"]Now I'm trying use a thread on the openCV and my program is closing with no specific error in the crash report i have this:
    @

    Thread 7 Crashed:
    0 OpenCV 0x0001209a GOMP_parallel_end + 58
    1 OpenCV 0x00012121 GOMP_parallel_start + 17
    2 OpenCV 0x00335108 cvSetImagesForHaarClassifierCascade + 1560
    3 ??? 0x0000002f 0 + 47
    @
    [/quote]

    I'm not 100% sure, but it smells like NULL pointer dereferencement (0+47 : 0 is base pointer, 47 is offset, sum of them equals 02f, which is invalid address and crashed your program). Now, I definitely lack code to tell more.[/quote]

    I suggest you debug your app and step through the code of the thread. Then you will see which call crashes.



  • regarding the programs that do the sum, so the first lacked the a.exec()?

    how can I debug my program properly? =/ this is weird because with no thread it runs good =/

    with qdebug I know that the app runs until void faceTracker::detectFaces( IplImage *img ), but after faces = ... i have no qDebug in the console showing



  • [quote author="MAMartins" date="1302685551"]regarding the programs that do the sum, so the first lacked the a.exec()?
    [/quote]

    Yes

    [quote author="MAMartins" date="1302685551"]
    how can I debug my program properly? =/ this is weird because with no thread it runs good =/

    with qdebug I know that the app runs until void faceTracker::detectFaces( IplImage *img ), but after faces = ... i have no qDebug in the console showing
    [/quote]

    Use an actual debugger (gdb, visual studio debugger), it allows to step and inspect stack, variables, memory.

    If it works in single thread context and not in multithread, classical causes are:

    • access to resources from more than one thread without protection (QMutex), leading to corrupted data
    • race conditions between threads (once again, due to missing synchronization)

    Edit: fixed list presentation. Please use * before a list item (or # for numbered lists), not a -; Andre



  • [quote author="MAMartins" date="1302685551"]regarding the programs that do the sum, so the first lacked the a.exec()?

    how can I debug my program properly? =/ this is weird because with no thread it runs good =/
    [/quote]

    Use QtCreator or MSVS etc and set breakpoints.
    If you need help on doing this, have a look at QtCreators docs. Usually it's by pressing "F9" on a line.

    Then call F5 to start debugging.



  • This project is pretty interesting, I always wanted to know how face detect algorithms work. Let me know when finished ;)



  • [quote author="Gerolf" date="1302694209"]
    [quote author="MAMartins" date="1302685551"]regarding the programs that do the sum, so the first lacked the a.exec()?

    how can I debug my program properly? =/ this is weird because with no thread it runs good =/
    [/quote]

    Use QtCreator or MSVS etc and set breakpoints.
    If you need help on doing this, have a look at QtCreators docs. Usually it's by pressing "F9" on a line.

    Then call F5 to start debugging.

    [/quote]
    I'll try to find my error using the debugger, i'll keep you guys posted :)



  • www.remnum.com

    this is a video about building opencv libs on windows and mac os you can download it
    with a project for opencv + qt using thread and it is run good try it



  • I do not know if this makes a difference or not(as it seems you are not using the signal/slot mechanism) but calling exec() within your run reimplementation allows the thread to run in its own event loop. This tends to prevent some memory allocation issues and allows for thread safe usage of signals and slots. Like i said I do not know if this will hold any relevance in your case.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.