Vlad Sebesan
Vlad Sebesan

Reputation: 1

Qt application crash when trying to delete Qimage Format_RGB888

I get a "HEAP CORRUPTION DETECTED: After normal block ... CRT detected that the application wrote to memory after end of heap buffer." when variable test gets destroyed. If I change the image size, the crash does not occur, I'm thinking it has something to do with memory alignment, but I cannot figure out what.

I am using the official Qt builds for MSVC 2017 64bit

#include <QCoreApplication>
#include <QImage>

QImage foo(int width, int height)
{
    QImage retVal(width, height, QImage::Format_RGB888);
    for (int i = 0; i < height; ++i) // read each line of the image
    {
        QRgb *lineBuf = reinterpret_cast<QRgb *>(retVal.scanLine(i));
        for (int j = 0; j < width; ++j)
        {
            lineBuf[j] = qRgb(0,0,0);
        }
    }
    return retVal;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    {
        QImage test = foo(5,5);
    }
    return a.exec();
}

Upvotes: 0

Views: 234

Answers (1)

Vlad Sebesan
Vlad Sebesan

Reputation: 1

As GM suggested in the comment above, the problem is that QRgb is a 32bit construct. I've changed the code replacing QRgb with a custom RGB struct

struct RGB {
   uint8_t r;
   uint8_t g;
   uint8_t b;
};

QImage foo(int width, int height)
{
    QImage retVal(width, height, QImage::Format_RGB888);
    for (int i = 0; i < height; ++i) // read each line of the image
    {
        RGB *lineBuf = reinterpret_cast<RGB *>(retVal.scanLine(i));
        for (int j = 0; j < width; ++j)
        {
            RGB tmp;
            //.... do stuff with tmp
            lineBuf[j] = tmp;
        }
    }
    return retVal;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    {
        QImage test = foo(5,5);
    }
    return a.exec();
}

Upvotes: 0

Related Questions