Why does the construction of a 'CImage' (GDI+) make the programm crash?

0

I wanted to try using the class CImage, which comes from GDI+, to load an image and draw it at some place in my Graphical User Interface.

However, the programm crashes as soon as I call CImage's constructor, it seems, displaying the error:

Unhandled exception at 0x7757D968 (ntdll.dll) in myProgramm.exe: 0xC0000005: Access violation writing location 0x00000014

If I choose to break the programm after the exception, I can see that the cursor, which indicate the current called function, is pointing some CImage's method :

CImage::CInitGDIPlus::IncreaseCImageCount()

That's a reason why I was supposing GDI+ initialization could be the cause of the problem. So I have written a sample to possibly identify the problem, trying whether to initialize GDI+ or not.

// main.cpp
// (Do not forget to link gdiplus.lib and activate "Use MFC in a shared DLL")

#include <afxwin.h> 
#include <afxdockablepane.h> // CImage seems to be unknown without this include. (My actual program uses it anyway.)
#include <Gdiplus.h>

void testWithoutGdipInititalization()
{
        CImage image ; // The programm seems to crash here, at the construction of the image...
}; // ...And not here, at the destruction of the image

void testWithGdipInititalization()
{
    // Initialize GDI+.
    Gdiplus::GdiplusStartupInput gdipStartupInput;
    ULONG_PTR gdipToken ;
    Gdiplus::GdiplusStartup(&gdipToken, &gdipStartupInput, NULL);

    {
        CImage image ; // The programm seems to crash here, at the construction of the image...
    } // ...And not here, at the destruction of the image

    // Close Gdiplus
    Gdiplus::GdiplusShutdown(gdipToken); 
};

int main(int argc, char *argv[])
{
    bool withInitialization = true ;

    if(withInitialization)
        testWithGdipInititalization();
    else
        testWithoutGdipInititalization();

    return EXIT_SUCCESS;
}

...And both fail the same way.

Does someboy know what I'm doing wrong ?

c++
mfc
gdi+
asked on Stack Overflow Jul 19, 2016 by Lorèloi

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0