Image 析构函数中的访问冲突 [英] Access violation in Image destructor

查看:34
本文介绍了Image 析构函数中的访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会说一个非常简单的程序..

#include #include 使用命名空间 Gdiplus;int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR cmdLine, int nShow){//Gdiplus 变量GdiplusStartupInput mGdiplusStartupInput;ULONG_PTR mGdiplusToken;GdiplusStartup(&mGdiplusToken, &mGdiplusStartupInput, NULL);位图 bitmap(L"left.bmp");GdiplusShutdown(mGdiplusToken);返回0;}

运行此示例时,我在此函数中的 GdiplusBitmap.h 中遇到访问冲突

内联图像::~图像(){DllExports::GdipDisposeImage(nativeImage);}

通过删除对 Bitmap bitmap(L"left.bmp"); 的调用,一切正常.. 我试图在 msdn 上找到一个简单的例子(例如靠近 位图构造函数,但没有找到任何东西.)

我错过了什么?

解决方案

您创建的 Bitmap 实例在调用关闭 GDI+ 后超出范围.所以当 Bitmap 被破坏时,它不能调用给定的 GdipDisposeImage 方法.

如果您在关闭 GDI+ 之前确保删除了 bitmap,错误应该会消失.

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?

解决方案

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

这篇关于Image 析构函数中的访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆