I'm creating a child window in another class, so I pass the hWnd and the hInstance of the parent into the function, where I'm creating the child window.
My problem now is, that the createWindow() function of the child window hangs up and I get an error message which says: "An exception has beend encountered. This may be caused by an extension".
Does someone know what this message means, or what I'm doing wrong?
Here is it, where I'm calling the child window in the message handler of the parent window, because I'm using a submenu with ID's.
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);
}
}
}
Initialize:
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;
}
Here is the code to reproduce the error: https://ufile.io/ddmj4
this
is the implicit first argument of every non-static class method. It is a pointer to the object on which the method is called. The error message you are getting means you have called a method on a nullptr
. Though your code is incomplete, the only line where this could happen is
m_CreateProjectMenu->Initialize(m_hWnd, m_hinstance);
You can verify this by using a debugger to check m_CreatePorjectMenu
's value right before the call is made, or by adding an assert(CreatePorjectMenu);
. For the latter, make sure you are compiling with assertions enabled.
As for how to solve it, I can't tell without knowing your project's structure. Some function must have the responsibility to initialize that object and you have to make sure it is called before your callback. Alternatively, if the initializer pattern doesn't work for whatever reason, your callback can check for nullptr
and create the object if necessary.