Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-25543

QImage: Assignment operator and copy constructor do not copy data when original image was created from raw pointer

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P2: Important
    • None
    • 4.8.1
    • GUI: Painting
    • None
    • Qt 4.8.1
      Compiler: Microsoft Visual Studio 2008, Version 9.0.30729.1 SP
      Os: Windows 7 SP1




    Description

      Assignment and copy constructor of QImages that get a QImage constructed from raw pointer (e.g. with QImage(uchar* data, int width, int height, Format format)) as parameter do not work as expected. Under certain circumstances invalid memory is accessed.

      Root cause:
      Due to implicit sharing the assignment operator and copy constructor of QImage do not copy image data, they only make a shallow copy.
      This is fine for normal QImages, but it is not fine for QImages created from raw data with a constructor like .

      In case of a QImage created from a raw pointer the shallow copy of the assignment operator and copy constructor only copy the raw data pointer to the new QImage and do not copy the data itself. So the raw memory pointer has to be valid as long as the copy lives. This is a big problem, because the documentation states that "The buffer must remain valid throughout the life of the QImage." which is not true. Correct would be that "The buffer must remain valid throughout the life of the QImage and throughout the life of all copies of that QImage that have not yet been modified."

      Consequences:

      • You cannot send QImages created that way over queued connections, because usually the raw data pointer is destroyed before the queued image is used. I attached a simple program that illustrates this.
      • You cannot pass the image to methods that make a copy for later use

      Proposed solution:
      The copy constructor and assignment operator have to copy the data instead of copying the data pointer when the original image was created from a raw pointer. This is the only way, because reference counting mechanisms will not work here due to the fact that the QImage is not the owner of the raw data.

      Related tickets:
      QTBUG-10200: The root cause of ticket QTBUG-10200 is the bug described here. I took the liberty of creating another ticket, because ticket QTBUG-10200 only describes the symptoms, not the real bug. This bug has a much higher severity and is not just documentation related as suggested by ticket QTBUG-10200.

      Example:
      Attached is a simple example that illustrates this bug. This example is correct according to QImage documentation, nevertheless invalid memory will be accessed.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              rodal Samuel Rødal
              erikhasslmeyer Erik Haßlmeyer
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes