otisonoza
otisonoza

Reputation: 1344

QImage out of memory

I have sub-classed QGLWidget and I show an image in it.

For small images (let's say 1200*1000) it works fine.

For bigger ones (10000 * 6000) it crashes.

MyGLWidget::MyGLWidget(QImage* image, QWidget *parent) : QGLWidget(parent)
{
    // ...
    _data = *image;
    _gldata = QGLWidget::convertToGLFormat(_data); // Crash at this point.
    // ...
}

Error:

QImage: out of memory, returning null image
ASSERT: "dst.depth() == 32" in file qgl.cpp, line 2094

I have 8GB of memory, the application takes max. 1GB.

What can I do?

EDIT

Ok, assuming that I run out of RAM, how do I catch this error?

try
{
    _gldata = QGLWidget::convertToGLFormat(_data);
}
catch(...)
{
    qDebug() << "e.what()";
}

It look's like it's not an exception.

Upvotes: 1

Views: 4463

Answers (2)

alexisdm
alexisdm

Reputation: 29886

There is no real error handling in convertToGLFormat, it just crashes with Q_ASSERT if you are out of RAM, or if the image doesn't have the right format.

As a workaround, you can try allocating an image with the same size as convertToGLFormat and if it succeeds, free the image and call the function, as it will likely succeed too, if not, just skip the image.

QImage dummy(_data.size(), QImage::Format_ARGB32);
if (!dummy.isNull()) {
    dummy = QImage(); // free the image
    _gldata = QGLWidget::convertToGLFormat(_data);
}

Upvotes: 0

peppe
peppe

Reputation: 22724

You're out of RAM.

And I'm not even sure QImage can handle such big images.

converToGLFormat doesn't allocate anything on the GPU, it just converts the image to the ARGB8888 format and with the right byte ordering, i.e. making it suitable for upload via glTex(Sub)Image2D, see here. In Qt 5.2 you could also consider QOpenGLTexture if you're dealing with textures, given the right GPU capabilities it may let you avoid this conversion step.

Upvotes: 2

Related Questions