c++openexr

FrameBuffer.insert() will result in a access violation during loading of EXR image


I have the following function to load a OpenEXR image, which is basically just copied from their examples:

void HDrRenderer::ReadExrImage(
    const char fileName[],
    Imf::Array2D<half>& rPixels,
    Imf::Array2D<half>& gPixels,
    Imf::Array2D<float>& zPixels,
    int& width, int& height)
{
    Imf::InputFile file(fileName);
    auto header = file.header();
    Imath::Box2i dw = header.dataWindow();

    width = dw.max.x - dw.min.x + 1;
    height = dw.max.y - dw.min.y + 1;

    rPixels.resizeErase(height, width);
    gPixels.resizeErase(height, width);
    zPixels.resizeErase(height, width);

    Imf::FrameBuffer frameBuffer;

    frameBuffer.insert("R",
        Imf::Slice(Imf::HALF,
            (char*)(&rPixels[0][0] - dw.min.x - dw.min.y * width),
            sizeof(rPixels[0][0]) * 1,
            sizeof(rPixels[0][0]) * width,
            1, 1, 0.0));

    frameBuffer.insert("G",
        Imf::Slice(Imf::HALF,
            (char*)(&gPixels[0][0] - dw.min.x - dw.min.y * width),
            sizeof(gPixels[0][0]) * 1,
            sizeof(gPixels[0][0]) * width,
            1, 1, 0.0));

    frameBuffer.insert("Z",
        Imf::Slice(Imf::FLOAT,
            (char*)(&zPixels[0][0] - dw.min.x - dw.min.y * width),
            sizeof(zPixels[0][0]) * 1,
            sizeof(zPixels[0][0]) * width,
            1, 1, FLT_MAX));

    file.setFrameBuffer(frameBuffer);
    file.readPixels(dw.min.y, dw.max.y);
}

My problem is rather straight forward: Any of the FrameBuffer.insert() methods will crash the program with the following message:

Exception thrown at 0x00007FFDE6BFC1AE (OpenEXR-3_1.dll) in MyAwesomeProgram.exe: 0xC0000005: Access violation reading location 0x0000000000000019.

I have already tried to separate the Slice -> the problem really is somewhere in the insert() method. I'm curious what the reason could be, how to find it and how to solve the issue.


Solution

  • I had the same problem - and I keep stumbling on it every time I come back to C++ after a break. In visual studio, you cannot run an application in debug mode while using dlls compiled in release mode. Other way is okay. Compile the OpenEXR library in debug mode, debug your app, then compile both in release. Hope this helps someone avoid a couple of days of teeth-gnashing!

    Cheers!