I have a funny error occurring at runtime where my HINSTANCE
and HWND
variables are getting corrupted. I have posted the debugging output below and the line of code where the runtime error occurs. *Note: even though the screen capture kindof shows the HWND isn't corrupted, it doesn't point to the right/valid window anymore
Why is this problem occurring and how can I fix it?
WindowTiler.exe!WindowLayoutComponent::init(const IEventArgs & evtArgs) Line 26 C++
[External Code]
WindowTiler.exe!EventDelegate::operator()(const IEventArgs & evtArgs) Line 21 C++
WindowTiler.exe!IApp::eventHandler(const int & evtId, const IEventArgs & evtArgs) Line 20 C++
WindowTiler.exe!Win32App::wndProc(HWND__ * hwnd, unsigned int message, unsigned int wParam, long lParam) Line 18 C++
[External Code]
[Frames below may be incorrect and/or missing, no symbols loaded for user32.dll]
WindowTiler.exe!Win32App::initInstance(const Win32AppInit & evtArgs) Line 154 C++
WindowTiler.exe!Win32App::init(const IEventArgs & evtArgs) Line 100 C++
WindowTiler.exe!App::init(const IEventArgs & evtArgs) Line 45 C++
WindowTiler.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 16 C++
[External Code]
Status WindowLayoutComponent::init(const IEventArgs& evtArgs)
{
auto args = (const WinEventArgs&)evtArgs;
HWND btn = CreateWindowEx(WS_EX_TRANSPARENT, _T("Button"), _T("Test"), WS_VISIBLE | WS_CHILD | WS_EX_TRANSPARENT,
30, 30, 50, 50, args.hwnd, NULL, args.hinstance, 0); // LINE 26
return S_SUCCESS;
}
Edit: There are 2 IEventArgs
objects involved. One passed to App::init(const IEventArgs & evtArgs)
and another different object created in Win32App::wndProc
and passed to IApp::eventHandler(const int & evtId, const IEventArgs & evtArgs)
.
LRESULT CALLBACK Win32App::wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
WinEventArgs args { hinstance, hwnd, wParam, lParam };
eventHandler(message, args);
...
args
variable is dead once the call toCreateWindowEx
occurs, so the values in the debugger ate not meaningful.auto args = ...
creates a copy on the stack.CreateWindowEx
the variableargs
is not used again anywhere else in the function so it's dead at that point. Since it's no longer being used the compiler could use its storage on the stack (or in registers) for something else. That should only happen with optimization turned on, so it shouldn't happen in debug builds. So the fact that you're seeing garbage values doesn't mean much. Without a complete example its impossible to say what your actual problem is.