Results 1 to 7 of 7

Thread: Crash while using threads

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Crash while using threads

    Hi! I'm experiencing some crashes that I can't explain. I'm using a QThread to generate previews of images taken from the disk. So, in my GUI thread, I create the new QThread thumbnailLoader, and I start it.

    The thumbnailLoader starts by iterating over all the files in the directory and creates the thumbnail with an algorithm I wrote, and then emits a signal imageReady. This signal has been connected before to a method in my GUI thread which sets the new QPixmap to the icon (which is a subclass of QGraphicsItem) which is redrawn with the new icon.

    The problem is that I'm experiencing crashes randomly, so I'm trying to figure out why. This is my run() method:

    Qt Code:
    1. void run() {
    2. // Load the image very quickly.
    3. QPixmap* thumbnail = new QPixmap(...);
    4. emit imageReady(thumbnail);
    5. }
    To copy to clipboard, switch view to plain text mode 

    the connected method is instead something like:

    Qt Code:
    1. void ContentScene::thumbnailGenerated(QPixmap* thumbnail) {
    2. icon->setThumbnail(thumbnail);
    3. }
    To copy to clipboard, switch view to plain text mode 

    setThumbnail assigns the pointer to the new pixmap and updated the item. In this situation, do you have any idea of what could be the cause of the crashes? The only thing I could think of is that my thread calls thumbnailGenerated(...) before the previous call was finished. Maybe I should use a semaphore to queue the calls?
    Thanks for any advice!

  2. #2
    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: Crash while using threads

    You can't operate on pixmaps in worker threads. You can only operate on images (QImage, not QPixmap).

  3. #3
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Crash while using threads

    Oh... this is very interesting... not a problem of semaphores then... Is there any place in the documentation where I can find why this is not possible?
    Thank you very much for your help, as usual! :-)

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Crash while using threads

    From Qt Documentation [Thread-support in Qt modules]:

    Painting in Threads

    QPainter can be used in a thread to paint onto QImage, QPrinter, and QPicture paint devices. Painting onto QPixmaps and QWidgets is not supported. On Mac OS X the automatic progress dialog will not be displayed if you are printing from outside the GUI thread.

  5. #5
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Crash while using threads

    Oh, ok, thanks, now I found it. This is quite a problem as I don't know how to avoid having to print to QPixmaps.

    The problem is that in my thread I load the images in a Windows HBITMAP, and then I get the QPixmap by using fromWinHBITMAP. I can't emit the signal using the HBITMAP, as I would have to register the HBITMAP, but I'm not sure if I can do that.

    So, I thought I could create a QImage instead, but that is not possible as far as I can understand, as I read that creating a QPixmap is required to create the QImage and this would result in more overhead I suppose (I need this code to be very efficient). So... this is quite a problem

    Thank you for your help, and if you had any idea, please suggest! Thanks again!

  6. #6
    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: Crash while using threads

    Quote Originally Posted by Luc4 View Post
    Oh, ok, thanks, now I found it. This is quite a problem as I don't know how to avoid having to print to QPixmaps.
    Use QImage instead, just as I told you.


    So, I thought I could create a QImage instead, but that is not possible as far as I can understand, as I read that creating a QPixmap is required to create the QImage
    No, that's not true. Even if it was, you could convert a pixmap to QImage in the main thread and then pass it to the worker thread.

  7. #7
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Crash while using threads

    Yes, I was trying to use QImage, but unfortunately I wasn't able to create the QImage from HBITMAP without using QPixmap
    Oh, so it was sufficient to create a method in the main thread which performed the conversion?
    Anyway, I solved by avoiding completely the use of HBITMAP. I loaded the image into a QImage directly and now I'm not seeing any crash anymore
    Thanks!

Similar Threads

  1. QGraphicsItem + Threads = Crash ?
    By medved6 in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2009, 04:13
  2. Threads in Qt
    By freekill in forum Qt Programming
    Replies: 4
    Last Post: 11th November 2009, 18:49
  3. Qt threads
    By ^Nisok^ in forum Newbie
    Replies: 12
    Last Post: 22nd April 2009, 14:35
  4. Threads...
    By Abc in forum Qt Programming
    Replies: 1
    Last Post: 19th June 2008, 17:35
  5. Once more: Threads in Qt4
    By high_flyer in forum Qt Programming
    Replies: 5
    Last Post: 9th August 2006, 18:35

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.