Access violation in Image destructor

旧时模样 提交于 2019-12-04 03:59:40

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/3354136/access-violation-in-image-destructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!