2017-09-29 79 views
-1

我在另一個類中創建了一個子窗口,所以我將父窗口的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

+0

您發佈的錯誤消息是由Visual Studio錯誤引起的。代碼似乎沒問題。目前還不清楚「這是nullptr」與問題內容有關。在哪裏'this'是'nullptr'? – VTT

+0

當我在parentmessagehandler中調用initializeWindow函數時,visual studio錯誤顯示出來,我真的不知道爲什麼,因爲這從來沒有發生過,所以我不知道是什麼導致了問題。我還搜索了對於這樣的錯誤網絡,但似乎沒有人有這個呢。 – MystikReasons

回答

-1

this是每個非的隱式第一個參數靜態類方法。它是一個指向調用該方法的對象的指針。您收到的錯誤消息意味着您在nullptr上調用了方法。雖然你的代碼是不完整的,在這種可能發生的唯一線路

m_CreateProjectMenu->Initialize(m_hWnd, m_hinstance); 

您可以通過使用調試器來檢查m_CreatePorjectMenu的值調用時權之前,或者是附加了assert(CreatePorjectMenu);驗證這一點。對於後者,請確保您正在編譯時啓用斷言。

至於如何解決它,我不知道你的項目的結構不知道。某些函數必須有責任初始化該對象,並且必須確保在回調之前調用它。另外,如果初始化模式不管用什麼原因,你的回調可以檢查nullptr並在必要時創建對象。

+0

謝謝你對錯誤的解釋。我在調用之前嘗試調試器,並且無法讀取內存。我可以用assert(CreateProjectMenu)驗證它,它已經把我扔出去並返回一個nullptr。 PS:我做了一個鏈接,我粘貼了代碼來重新創建問題,也許你必須刪除一些不必要的代碼,但是我希望不要。 – MystikReasons

+0

[mcve]需要在問題*中發佈*,而不僅僅是鏈接到.zip文件。但是導致這種情況的最常見的錯誤是在調用CreateWindow之後初始化'm_CreateProjectMenu' *,可能是因爲錯誤的印象是,直到你啓動消息泵時纔會得到任何窗口消息。 –

相關問題