Results 1 to 11 of 11

Thread: this pointer

  1. #1
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default this pointer

    I'm running in an application crash. gdb's complaining about segmentation fault in QObject::thread (this=(incomplete type)). Here's the code where I suspect the problem is coming from:

    Qt Code:
    1. #include <QtDebug>
    2. #include <QtGUI>
    3. #include <QPainter>
    4. #include <QThread>
    5. #include "webcam.h"
    6. #include "capturingthread.h"
    7. #include "imagebuffer.h"
    8. #include "renderingthread.h"
    9.  
    10. WebcamWidget::WebcamWidget(QWidget *parent) : QWidget(parent)
    11. {
    12. setupUi(this);
    13.  
    14. pWaitCondition = &waitCondition;
    15. imageBuffer = new ImageBuffer(pWaitCondition);
    16. capturingThread = new CapturingThread(imageBuffer);
    17. renderingThread = new RenderingThread(imageBuffer,pWaitCondition);
    18.  
    19. qRegisterMetaType<QImage>("QImage&");
    20. connect(renderingThread, SIGNAL(renderingDone(QImage& )), this, SLOT(updatePixmap(QImage& )));
    21. connect(startButton, SIGNAL(clicked()), this, SLOT(startThreads()));
    22. }
    23.  
    24. WebcamWidget::~WebcamWidget()
    25. {
    26. delete imageBuffer;
    27. delete capturingThread;
    28. delete renderingThread;
    29. }
    30.  
    31. void WebcamWidget::startThreads()
    32. {
    33. if (capturingThread->isRunning())//&&renderingThread->isRunning())
    34. {
    35. capturingThread->haltCapture();
    36. renderingThread->haltRendering();
    37. startButton->setText("Start");
    38. }
    39. else
    40. {
    41. capturingThread->start();
    42. renderingThread->start();
    43. startButton->setText("Stop");
    44. }
    45. }
    46.  
    47. void WebcamWidget::haltThreads()
    48. {
    49. capturingThread->haltCapture();
    50. renderingThread->haltRendering();
    51. }
    52.  
    53. void WebcamWidget::paintEvent(QPaintEvent*)
    54. {
    55. QPainter painter(this);
    56. painter.fillRect(rect(), Qt::black);
    57. if (pixmap.isNull())
    58. {
    59. qDebug()<<"W: Pixmap data null.";
    60. painter.setPen(Qt::black);
    61. painter.drawText(rect(), Qt::AlignCenter, "No Image Feed");
    62. return;
    63. }
    64. painter.drawPixmap(10,10,pixmap);
    65. qDebug()<<"Painting successful.";
    66. }
    67.  
    68. void WebcamWidget::updatePixmap(QImage& image)
    69. {
    70. qDebug()<<"WebcamWidget::updatePixmap() invoked.";
    71. pixmap = QPixmap::fromImage(image);
    72. update();
    73. }
    To copy to clipboard, switch view to plain text mode 

    I only used this in this .cpp file and I don't see this anywhere in my spawned threads' codes. Did I not use it right? TIA.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: this pointer

    gdb's complaining about segmentation fault in QObject::thread (this=(incomplete type))
    gdb will also tell you on which line it seg faults, which can give a better clue to what is wrong.
    It sounds more like a problem in one of your threads.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: this pointer

    Sending a reference to an image across threads is what causes the problem. You have to send a const reference or a copy instead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: this pointer

    Sending a reference to an image across threads is what causes the problem. You have to send a const reference or a copy instead.
    Whoa! This actually worked like magic! Just curious though, the program now runs however, when I use gdb I still get a segmentation fault.
    0x7c96df51 in ntdll! RtlpNtMakeTemporaryKey ()
    from C:\WINDOWS\system32\ntdll.dll


    I googled for the error and it points out to be internal to Windows dlls. How could I have accessed such? And why still the error in gdb?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: this pointer

    are you protecting your image with mutexes?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: this pointer

    Quote Originally Posted by high_flyer View Post
    are you protecting your image with mutexes?
    No. I'm actually capturing images real-time through a webcam. But I made it multithreaded. I have a dedicated thread querying the webcam for a new image (which should happen every 30ms for typical webcams). When querying is successful, I place the image in a buffer and I wake up the rendering thread. The rendering thread converts the OpenCV's IplImage format to Qt's QImage format then painting happens.

    Based on the clockings I'm currently observing, the conversion process (IplImage to QImage) along with the painting, takes some time and I get to skip some images captured by the webcam. This is acceptable though, as long as the latest image in the buffer gets to be displayed on screen. My image buffer contains only the latest frame captured by the webcam instead of stacking up the images in a queue.

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: this pointer

    No
    You really should!
    Even if its not the only problem in your application, it is a major one!
    You have to protect mutual exclusive data, otherwise you get situations in which two threads are accessing the same resource at the same time - and that is "not good"!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: this pointer

    I see, thanks for the advice. But which data do you think I need to lock/unlock?
    1. Write to image buffer? (Only a single thread is writing on to the buffer)
    2. Read to the image buffer? (Might be, 2 threads are reading the same image in the buffer but I feel it's not an issue reading data at the same time)
    3. Update of QPixmap data before painting? (I believe not the case, because even if I get to update it on and on, multiple calls to update() is fine.)

    If in the case of 1 and 2, I ran into some trouble as I could not unlock the mutex after function return:
    Qt Code:
    1. IplImage* ImageBuffer::getFrame()
    2. {
    3. QReadLocker readLocker(&rwLock);
    4. return image; //rwLock fails to be unlocked on function return
    5. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: this pointer

    Maybe you should use 2 image buffers:
    - Renderer locks buffer #1 to read and render; you capture to buffer #2.
    - As soon as the renderer is ready it unlocks buffer #1 and the capture goes to buffer #1 while releasing buffer #2 to be rendered.

    So at the start of every capture you check which buffer is safe to use.
    The renderer has to wait until the other buffer is released. Only in that case there is a new image availbale, because an image can never be written to the just rendered buffer.

    Let us know if it actually works...

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: this pointer

    But which data do you think I need to lock/unlock?
    Any data which is accessed by more then one thread needs to be protected.
    1. Write to image buffer? (Only a single thread is writing on to the buffer)
    Yes, but if it is the SAME buffer that is being accessed by other threads, it has to be protected, even more so if you have references to this elements in other places (and you have).
    Threaded programming can get very messy and very hard to debug if you don't understand the principals and traps associated with it.
    You should really read some tutorials, and get to know threaded programming.
    Its not trivial, and the way to think about it is different then non threaded programming.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: this pointer

    Thanks high_flyer, I'll take those pointers.

    @boudie
    I'm very much open to your suggestion. Using two buffers could actually speed up data access as I could read and write simultaneously without worrying to much about having my buffer blocked. However, I'm also using the buffer for synchronization. My rendering thread goes to sleep whenever it is finished with its rendering. I wake up the thread when a new image has been placed in the buffer. I do this using a wait condition. I feel I could not implement my current idea of sleeping threads with this suggestion.

Similar Threads

  1. Pointer to Pointer
    By ^NyAw^ in forum General Programming
    Replies: 5
    Last Post: 25th April 2009, 08:00
  2. pointer and iterator
    By mickey in forum General Programming
    Replies: 6
    Last Post: 3rd February 2008, 22:24
  3. using the parent-pointer
    By doktorn in forum Newbie
    Replies: 5
    Last Post: 31st August 2007, 18:59
  4. pointer
    By mickey in forum General Programming
    Replies: 4
    Last Post: 1st September 2006, 00:42
  5. get Parent Pointer
    By raphaelf in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2006, 13:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.