我有以下 C++ 行,它从 GDI 的设备上下文中获取 GDI+ 句柄:
//HDC hDC = device context from GDI
if(hDC)
{
//Obtain graphics handle for GDI++
Graphics* pgrpx = new Graphics(hDC); //I get invalid memory exception here
//Code continues
//...
//Free object
delete pgrpx;
pgrpx = NULL;
}
在我的测试中它运行得很好。
但最近我收到了应用程序的崩溃转储,在我在代码中上面指出的行中出现了“它无法访问的线程引用的内存位置”异常 - 就在应该初始化 Graphics 对象的位置.
这里可能出现什么问题?又该如何解决呢?
最佳答案
你检查过GDI+的初始化吗?我不确定问题是否出在此处,但你可以尝试一下。
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
if(GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) != Gdiplus::Ok)
{
MessageBox(NULL, TEXT("GDI+ failed to start up!"),
TEXT("Error!"), MB_ICONERROR);
return -1;
}
关于c++ - GDI+ 初始化使应用程序崩溃,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13151956/