views:

25

answers:

1

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?

+2  A: 

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+.

Jeff Yates
+1 this. Add a pair of parenthesis around the bitmap to force it out of scope, or make it a pointer type (ugly).
Mike Caron
That makes sense. But how do I explicitly destroy a bitmap that is created on the stack?
Default
@Mike: so kind of a block scope around the Bitmap call.. That actually solved it. sweet :) Thanks for the quick replies
Default
@Michael: Yup. By limiting the scope of where your objects are bound, you can force them off the deep end ;)
Mike Caron