4

The Qt class QImage has two versions of its bits() function that returns a pointer to the underlying image data. One is const, the other is not. Here is the documentation for the non-const version:

Returns a pointer to the first pixel data. This is equivalent to scanLine(0).

Note that QImage uses implicit data sharing. This function performs a deep copy of the shared pixel data, thus ensuring that this QImage is the only one using the current return value.

The return type is uchar*.

Does this imply that I'm responsible for calling delete on this pointer when I'm done with it to avoid a memory leak?

Tim MB
  • 4,413
  • 4
  • 38
  • 48

1 Answers1

6

No, it just means that the non-const version causes that QImage to detach from any other instances which share the same data, since you might be about to modify it. It still maintains ownership. Just to be sure, the implementation (from Qt 4.7.2):

uchar *QImage::bits()
{
    if (!d)
        return 0;
    detach();
    // In case detach ran out of memory...
    if (!d)
        return 0;
    return d->data;
}
Dan Milburn
  • 5,600
  • 1
  • 25
  • 18
  • And one reason why the copying of the pixels is mentioned in the documentation is that it may take noticeable amount of time and memory if the image is very large. –  Feb 14 '13 at 16:14