WIN32应用程序崩溃,具体取决于用于访问字符串的方法[重复]

问题描述 投票:0回答:1
这里是我窗口的片段。cpp文件

bool Window::show(const GUI::Application *const app) { WNDCLASSEXW windowClass = { 0 }; windowClass.cbSize = sizeof(WNDCLASSEXW); windowClass.lpfnWndProc = m_WindowProc; windowClass.hInstance = app->getInstance(); // Load standard cursor if (!(windowClass.hCursor = ::LoadCursorW(NULL, IDC_ARROW))) { return false; } windowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1); windowClass.lpszClassName = app->getAppId().c_str(); // Register window class with system if (!::RegisterClassExW(&windowClass)) { return false; } OutputDebugStringW(L"Registered window class"); // Actually create the window m_handle = ::CreateWindowExW( 0, windowClass.lpszClassName, m_title.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, m_width, m_height, NULL, NULL, app->getInstance(), NULL ); if (!m_handle) { DisplayErrorMessage(GetLastError()); return false; } OutputDebugStringW(L"Created window"); ::ShowWindow(m_handle, app->getShowCmd()); OutputDebugStringW(L"ShowWindow executed, going into message loop"); MSG msg = { 0 }; while (::GetMessageW(&msg, NULL, 0, 0)) { ::TranslateMessage(&msg); ::DispatchMessageW(&msg); } // Cleanup resources ::UnregisterClassW(windowClass.lpszClassName, app->getInstance()); return true; }

如果我在第28行上更改为

windowClass.lpszClassName
,则该应用程序用错误代码1407崩溃。
为什么会发生这种情况?他们正在访问完全相同的数据,并且在第15行的第一个呼叫和第28行上的第二个调用之间没有修改字符串。 请注意:我知道与该主题有关的其他论坛上也存在问题,但是这些答案实际上都没有解释为什么一行奏效另一行,这是我在此问题中所寻找的。

遵循
mmsdn
,第二个参数是类型

app->getAppId().c_str()

,它代表着

gong的
p

ointer,to

c

onstant
c++ winapi
1个回答
2
投票
app->getAppId().c_str()

部分清楚地指出,您必须提供在函数调用期间有效的指针。当您从返回的临时对象中传递ACreateWindowExW时,情况并非如此。

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.