Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access violation in Image destructor

A very simple program I might say..

#include <windows.h>
#include <gdiplus.h>

using namespace Gdiplus;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR cmdLine, int nShow){
    // Gdiplus variables
    GdiplusStartupInput mGdiplusStartupInput;
    ULONG_PTR           mGdiplusToken;
    GdiplusStartup(&mGdiplusToken, &mGdiplusStartupInput, NULL);

    Bitmap bitmap(L"left.bmp");


    GdiplusShutdown(mGdiplusToken);
    return 0;
}

When running this example I get an access violation in GdiplusBitmap.h in this function

inline 
Image::~Image()
{
    DllExports::GdipDisposeImage(nativeImage);
}

By removing the call to Bitmap bitmap(L"left.bmp"); everything works fine.. I tried to find a simple example on msdn (for instance somewhere near the Bitmap constructor, but didn't find anything.)

What am I missing?

like image 711
default Avatar asked Feb 27 '23 14:02

default


1 Answers

The Bitmap instance you created is falling out of scope AFTER the call to shutdown GDI+. So when the Bitmap gets destructed, it cannot call the given GdipDisposeImage method.

The error should go away if you make sure that bitmap is deleted before you shutdown GDI+.

like image 184
Jeff Yates Avatar answered Mar 03 '23 22:03

Jeff Yates