我的WinMain就是这样开始的:
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
if(SUCCEEDED(CoInitialize(NULL)))
{
{
HRESULT hr = S_OK;
Game game;
D2DResources d2DResources;
game.SetPointer(d2DResources);
hr = d2DResources.Initialize(hInst);
我的编译器将在打破未发送的异常之前执行上面显示的行。我知道它不会更进一步,因为我在hr = d2DResources.Initialize(hInst)之前添加了一个MessageBox命令,之后又添加了另一个,只有第一个出现。
所以,D2DResources :: Initialisize(HINSTANCE)看起来像这样:
HRESULT D2DResources::Initialize(HINSTANCE hInst)
{
HRESULT hr;
// Create factory
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
// Create WIC factory
if(SUCCEEDED(hr))
{
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pIWICIF)
);
}
// Create a window class
WNDCLASSEX wClass;
ZeroMemory(&wClass,sizeof(WNDCLASSEX));
wClass.cbClsExtra=NULL;
wClass.cbSize=sizeof(WNDCLASSEX);
wClass.cbWndExtra=NULL;
wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wClass.hIcon=NULL;
wClass.hIconSm=NULL;
wClass.hInstance=hInst;
wClass.lpfnWndProc=WinProc;
wClass.lpszClassName="Window Class";
wClass.lpszMenuName=NULL;
wClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClassEx(&wClass))
{
int nResult=GetLastError();
MessageBox(NULL,"Failed to register window class","Window Class Failed",MB_ICONERROR);
}
m_hWnd=CreateWindowEx(NULL,
"Window Class",
"Game", // Replace with gameName
WS_OVERLAPPEDWINDOW|WS_MAXIMIZE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
this);
if(!m_hWnd)
{
int nResult=GetLastError();
MessageBox(NULL,"Window class creation failed","Window Class Failed",MB_ICONERROR);
}
RECT rc;
GetClientRect(m_hWnd,&rc);
// Creates render target
if(SUCCEEDED(hr))
{
pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
m_hWnd,
D2D1::SizeU(
rc.right - rc.left,
rc.bottom - rc.top)),
&pRT);
}
D2D1_SIZE_F pRTSize = pRT->GetSize();
RECT rect = {50, (long)pRTSize.height-(LogLineSize*5), (long)pRTSize.width, (long)pRTSize.height};
logArea = rect;
if(SUCCEEDED(hr))
{
pRT->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&pWhiteBrush
);
}
if(SUCCEEDED(hr))
{
hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(pWF),
reinterpret_cast<IUnknown**>(&pWF)
);
}
if(SUCCEEDED(hr))
{
hr = pWF->CreateTextFormat(
L"Verdana",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
14.0f,
L"",
&pTextFormat
);
}
return hr;
}
重载的Game :: SetPointer在使用D2DResources对象调用时看起来像这样:
void Game::SetPointer(D2DResources& p)
{
pD2DResources=&p;
}
pD2DResources是D2DResources *。
当WM_SIZE消息发送到我的WinProc时,问题发生在D2DResources :: Initialization(HINSTANCE)期间。所以,有一些我的WinProc:
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg==WM_CREATE)
{
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
Game* pGame = (Game*)pcs->lpCreateParams;
::SetWindowLongPtrW(
hWnd,
GWLP_USERDATA,
PtrToUlong(pGame)
);
}
else
{
Game* pGame = reinterpret_cast<Game*>(static_cast<LONG_PTR>(
::GetWindowLongPtrW(
hWnd,
GWLP_USERDATA
)));
switch(msg)
{
case WM_SIZE:
{
UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam);
pGame->pD2DResources->OnResize(width, height);
}
break;
调用OnResize时,会出现问题。
void D2DResources::OnResize(UINT width, UINT height)
{
if(pRT)
{
HRESULT hr = pRT->Resize(D2D1::SizeU(width, height));
if(hr!=0) MessageBox(NULL,"Render target could not be resized","ID2D1HwndRenterTarget Error",MB_ICONERROR);
}
}
编译器中断并且有一个黄色箭头指向if(pRT)(pRT是指向渲染目标的指针)行。
有趣的是,在调试过程中查看autos会显示出来。
那么,问题是什么,解决方案是什么?
答案 0 :(得分:3)
当调用WinProc时,您在WinMain中声明的D2DResources对象已超出范围,但您仍然持有指向它的指针。当您通过指针调用方法时,它正在针对堆栈中不再指向D2DResources对象的位置执行(注意 - 您的MessageBox会误导您 - 它实际上超出了范围。)
解决此问题的最简单方法是更改
D2DResources d2DResources;
在WinMain中
D2DResources& d2DResources = *(new D2DResources());
将把相同的对象放在堆上。
虽然我不会发誓这是你代码的唯一问题......
答案 1 :(得分:2)
您将错误的指针传递给您的窗口。您正在D2DResources::Initialize
创建窗口并将this
作为lpParam
传递,但在窗口过程中,您将其转换为Game*
。