PySide QImage constructor from buffer doesn't copy memory



  • Hello all

    When I create a QImage from a buffer and then delete the buffer (or the buffer goes out of scope), the QImage object refers to an unreserved piece of memory. This leads to unexpected behavior as illustrated by the program below. The program saves two blue images whereas I would expect "savedimg1.png" to be red.

    I think this is a bug but I thought I'd ask here first before I submit a bug report. BTW, PyQt4 does save a red and blue image.

    @
    import sys

    if True:
    from PySide import QtGui
    IMG_FORMAT = QtGui.QImage.Format.Format_RGB32
    else:
    from PyQt4 import QtGui
    IMG_FORMAT = QtGui.QImage.Format_RGB32

    WIDTH = 255
    HEIGHT = 128

    def create_buf(r, g, b):
    color = chr(b) + chr(g) + chr(r) + chr(255)
    pixels = (WIDTH*HEIGHT) * color
    return buffer(pixels)

    app = QtGui.QApplication(sys.argv)

    buf1 = create_buf(255, 0, 0)
    img1 = QtGui.QImage(buf1, WIDTH, HEIGHT, IMG_FORMAT)
    print "buf1: {!r}".format(buf1)

    del buf1 # Removing this line will cause savedimg1.png to be red

    buf2 = create_buf(0, 0, 255)
    print "buf2: {!r}".format(buf2)
    img2 = QtGui.QImage(buf2, WIDTH, HEIGHT, IMG_FORMAT)

    print "img1 buffer: {!r}".format(img1.constBits())
    print "img2 buffer: {!r}".format(img2.constBits())

    img1.save('savedimg1.png')
    img2.save('savedimg2.png')
    @



  • Forgot to mention: I'm using Python 2.7.3 and PySide 4.8.4 on OS-X 10.6.8


  • Lifetime Qt Champion

    Hi,

    AFAIK, when you use the constructor variant with the buffer, you must ensure the buffer exists through all the lifetime of the QImage. Check the QImage documentation.


Log in to reply
 

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