Win32 CreateWindow()error,this is nullptr
我在另一个类中创建了一个子窗口,所以我将父窗口的hWnd和hInstance传递给了我创建子窗口的函数。Win32 CreateWindow()error,this is nullptr
我现在的问题是,子窗口的createWindow()函数挂起,我收到一条错误消息,它说:“异常已遇到,可能是由扩展造成的。
有人知道这个信息是什么意思,或者我做错了什么?
在这里,我在父窗口的消息处理程序中调用子窗口,因为我使用的是具有ID的子菜单。
LRESULT CALLBACK System::MessageHandler(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
MainMenu mMainMenu;
switch (message)
{
case WM_COMMAND:
{
switch (LOWORD(wparam))
{
//If user presses on the exit button
case IDM_FILE_EXIT:
{
PostQuitMessage(0);
} break;
case IDM_NEW_NEWPROJECT:
{
//////////////////////////////////////////////
// Here is the error showing up
//////////////////////////////////////////////
m_CreateProjectMenu->Initialize(m_hWnd, m_hinstance);
}break;
default:
break;
}
}
// Any other messages send to the default message handler as our application won't make use of them.
default:
{
return DefWindowProc(hwnd, message, wparam, lparam);
}
}
}
初始化:
bool CreateProjectMenu::Initialize(HWND m_ParentWindow, HINSTANCE m_hParentInstance)
{
//Initialize the window
InitializeWindow(m_ParentWindow, m_hParentInstance);
return true;
}
InitializeWindow:
void CreateProjectMenu::InitializeWindow(HWND m_ParentWindow, HINSTANCE m_hParentInstance)
{
wc.style = CS_HREDRAW | CS_VREDRAW; // Defines additional elements of the window class.
wc.lpfnWndProc = ChildProc; // A pointer to the window procedure.
wc.cbClsExtra = 0; // The number of extra bytes to allocate following the window-class structure.
wc.cbWndExtra = 0; // The number of extra bytes to allocate following the window instance.
wc.hInstance = m_hParentInstance; // A handle to the instance that contains the window procedure.
wc.hIcon = LoadIcon(wc.hInstance, IDI_APPLICATION); // Load the icon for the application.
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load the cursor for the application.
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // Load the background for the application.
wc.lpszMenuName = NULL; // Pointer to a character string that specifies the name of the resource class menu.
wc.lpszClassName = m_ProjectMenuWindowName; // Set the name for the window.
wc.hInstance = m_hParentInstance;
if (!RegisterClass(&wc))
{
MessageBox(NULL, L"Failed to register the menuprojectwindow", L"Error", MB_OK);
}
m_NewProjectHwnd = CreateWindow(NULL,
m_ProjectMenuWindowName,
WS_CHILD | WS_VISIBLE | WS_CAPTION
| WS_SYSMENU | WS_THICKFRAME
| WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
screenWidth, screenHeight,
m_ParentWindow,
NULL,
m_hParentInstance,
NULL);
// Check if the hwnd is zero(error)
// Display a messagebox with a error
if (m_NewProjectHwnd == 0)
MessageBox(NULL, L"Could not create the create project hwnd.", L"Error", MB_OK);
else
{
ShowWindow(m_NewProjectHwnd, SW_SHOW); // Bring the window up on the screen
SetFocus(m_NewProjectHwnd);
}
return;
}
这里是重现该错误的代码: https://ufile.io/ddmj4
this
是每个非的隐式第一个参数静态类方法。它是一个指向调用该方法的对象的指针。您收到的错误消息意味着您在nullptr
上调用了方法。虽然你的代码是不完整的,在这种可能发生的唯一线路
m_CreateProjectMenu->Initialize(m_hWnd, m_hinstance);
您可以通过使用调试器来检查m_CreatePorjectMenu
的值调用时权之前,或者是附加了assert(CreatePorjectMenu);
验证这一点。对于后者,请确保您正在编译时启用断言。
至于如何解决它,我不知道你的项目的结构不知道。某些函数必须有责任初始化该对象,并且必须确保在回调之前调用它。另外,如果初始化模式不管用什么原因,你的回调可以检查nullptr
并在必要时创建对象。
谢谢你对错误的解释。我在调用之前尝试调试器,并且无法读取内存。我可以用assert(CreateProjectMenu)验证它,它已经把我扔出去并返回一个nullptr。 PS:我做了一个链接,我粘贴了代码来重新创建问题,也许你必须删除一些不必要的代码,但是我希望不要。 – MystikReasons
[mcve]需要在问题*中发布*,而不仅仅是链接到.zip文件。但是导致这种情况的最常见的错误是在调用CreateWindow之后初始化'm_CreateProjectMenu' *,可能是因为错误的印象是,直到你启动消息泵时才会得到任何窗口消息。 –
您发布的错误消息是由Visual Studio错误引起的。代码似乎没问题。目前还不清楚“这是nullptr”与问题内容有关。在哪里'this'是'nullptr'? – VTT
当我在parentmessagehandler中调用initializeWindow函数时,visual studio错误显示出来,我真的不知道为什么,因为这从来没有发生过,所以我不知道是什么导致了问题。我还搜索了对于这样的错误网络,但似乎没有人有这个呢。 – MystikReasons