[Interest] Signal with QString argument from another thread.

Matthew Woehlke mwoehlke.floss at gmail.com
Wed Oct 7 16:14:48 CEST 2015


On 2015-10-06 16:42, Igor Mironchik wrote:
> But I had next experience. QImage is implicitly shared too. But I had 
> following issue:
> 
> I had a signal like this
> 
> void mySignal( QImage );
> 
> and when I tried to use this image in the slot in another thread (queued 
> connection was used) image was corrupted. Any operation with image 
> crashed application...
> 
> I have to say that image object created in method that emitted 
> mySignal() on the stack, like this:
> 
> void method( const QVideoFrame & frame )
> {
>      QImage image( constructed from frame data );

This right here is probably your problem. Check the documentation of
QImage; when constructed from a bag of bytes, the QImage DOES NOT¹ copy
or take ownership of the data. So, here...

>      emit mySignal( image );
> }

...you have to ensure that "frame data" remains valid until no copies of
the created image exist any more.

(¹ From <http://doc.qt.io/qt-5/qimage.html#QImage-4>: "The buffer must
remain valid throughout the life of the QImage and all copies that have
not been modified or otherwise detached from the original buffer. The
image does not delete the buffer at destruction.")

> This issue was solved by copying image like this:
> 
> emit mySignal( image.copy( image.rect() ) );

This works because you've forced a copy of your data to be made, so now
the QImage you are passing around owns the data.

> This problem occurs on Windows Surface only ( Qt 5.4.2 ).

Timing issue. You're just lucky on other platforms, but it's still
broken. Try running it under valgrind on Linux and see if you get use of
freed memory errors.

-- 
Matthew




More information about the Interest mailing list