2

I have the following C++ line that obtains the GDI+ handle out of GDI's device context:

//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;
}

It works just fine in my tests.

But recently I received a crash dump for the app, with the "thread referenced memory location that it has no access to" exception at the line I pointed above in the code -- right where the Graphics object is supposed to be initialized.

What could be the issue here? And how to address it?

4

1 回答 1

2

你检查 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;
}
于 2012-11-28T16:45:16.990 回答