[quote author="przybysh" date="1330692429"][quote author="Volker" date="1330691822"]
@przybysh:
It's stated nowhere that loadFromData() requires the uchar array to be valid dring the life time of the QImage object. The source code doesn't suppose that either. The overload that takes a QByteArray just calls the uchar version, so anything that holds true for the one does so for the other too.[/quote]
I agree that it's not stated explicitly. Later today I will provide example which proves that loadFromData() requires valid data during life time. For me loadFromData acts just like
bq. QImage ( const uchar * data, int width, int height, Format format )
[/quote]
OK, I took a look at the code, and my conclusions are that loadFromData follows a completely different code path than the constructor version, but that indeed the QByteArray version is the same as the uchar* version (in quite a weird way, actually[1]).
The loadFromData version creates a QBuffer, and then uses the normal image loading code (QImageReader) to create a new image, which is then assigned to this using an explicit operator=() call. My conclusion is that the data does not need to stay valid after that.
[quote author="phantom23" date="1330745543"]I have two follow-up questions if it is not too much of a hassle:
If QImage does indeed create a deep copy of the image data, should I assume that the destructor will release the memory? Or should I create a QByteArray, copy the image data into that and release it myself when the iamge is no longer needed?
[/quote]
QByteArray will take care of clearing up its own data. There is no need for manual memory management here. QImage using this path indeed will own the data it stores the image in.
[quote]
I also have a question related to QTextDocument::addResource(). When I'm done with the image, should I re-set the resource name using a null QVariant, or is that not necessary?[/quote]
I'm not sure what you mean by this exactly.
[1] The QByteArray version calls the uchar* version, which in turn creates a QByteArray from the raw data. Doing it the other way around would have been more logical, IMHO.