0xC0000005:执行CStatic.Create时访问冲突读取位置0x4fa2b3f1

时间:2011-07-08 05:53:53

标签: visual-studio-2008 visual-c++ exception access-violation

我对CStatic有疑问。所以我有这个:

    CStatic * lblPresent;
    SetWindowPos(NULL,GetSystemMetrics(SM_CXSCREEN)/2-234,0,0,0,SWP_NOSIZE | SWP_NOZORDER);
    lblPresent=new CStatic();
    wstring wtemp=L"Welcome";
    tempChar = new WCHAR[wtemp.length()+1];
    wcscpy_s(tempChar, wtemp.size()+1, (LPWSTR)wtemp.c_str());
    lblPresent->Create(tempChar, WS_CHILD | WS_VISIBLE,CRect(20, 90, 448, 130), this);

当它到达最后一行时,它说:

Unhandled exception at 0x6e54ba20 in CPTest.exe: 0xC0000005: Access violation reading location 0x4fa2b3f1.

当我跨过使用调试器时,它将我带到了这个:

AfxWndProcDllStatic(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
    AFX_MANAGE_STATE(&afxModuleState);
    return AfxWndProc(hWnd, nMsg, wParam, lParam);
}

异常似乎发生在返回的那一行,其中包含值(写在手表中):

        hWnd    0x001a01d0 {unused=0 }  HWND__ *
        lParam  0                       long
        nMsg    272                     unsigned int
        wParam  2425038                 unsigned int

有谁知道会发生什么以及该怎么做?

谢谢, Reinardus

1 个答案:

答案 0 :(得分:0)

为什么你不能这么简单:

lblPresent->Create(L"Welcome", WS_CHILD | WS_VISIBLE,CRect(20, 90, 448, 130), this);

this指针是否有效并实际指向某个有效的CWnd?