Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

DestroyWindow(HWND) gives me exceptions... - on x64...

$
0
0

Hello

I hope I'm posting in the correct forum. I'm working on a project and I noticed that I kept getting the following kinds of exceptions:

"First-chance exception at 0x76de7a24 in tmpxXx.exe: 0xC0000005: Access violation reading location 0x00572120."

And I think I managed to track it down to the DestroyWindow(HWND) function. So I made a very simple application just to try this out:

 

#include <Windows.h>

LRESULT CALLBACK winproc(HWND hWnd, UINT uiMSG, WPARAM wPar, LPARAM lPar)
{
	switch (uiMSG)
	{case WM_CLOSE:
		DestroyWindow(hWnd);return 0;case WM_DESTROY:
		PostQuitMessage(0);return 0;
	}return DefWindowProcW(hWnd, uiMSG, wPar, lPar);
}int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPWSTR szCmdLine, int iCmdShow)
{
	MSG msg;
	HWND hWnd = NULL;
	WNDCLASSW wc = {0};

	wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
	wc.hCursor		 = (HCURSOR) LoadCursorW(NULL, IDC_ARROW);
	wc.hIcon		 = (HICON)		LoadIconW(NULL, IDI_APPLICATION);
	wc.hInstance	 = hInst;
	wc.lpfnWndProc	 = winproc;
	wc.lpszClassName = L"bahhh";
	wc.style		 = CS_HREDRAW | CS_VREDRAW;

	RegisterClass(&wc);

	hWnd = CreateWindowW(wc.lpszClassName, L"Test Window", WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, NULL, NULL, hInst, NULL);

	ShowWindow(hWnd, iCmdShow);
	UpdateWindow(hWnd);

	msg.message = WM_NULL;

	while(GetMessageW(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessageW(&msg);
	}return msg.wParam;
}

When I click the close button on the window the winproc function handles the WM_CLOSE message by calling the DestroyWindow(hWnd) function. DestroyWindow() in turn sends a WM_DESTROY message to the winproc which PostQuitMessage(0) and returns back into the WM_CLOSE case since the DestroyWindow() function has finished processing. As DestroyWindow() returns I get four exceptions which looks like this:

"First-chance exception at 0x76de7a24 in tmpxXx.exe: 0xC0000005: Access violation reading location 0x00572120.
First-chance exception at 0x76de7a24 in tmpxXx.exe: 0xC0000005: Access violation reading location 0x00572120.
First-chance exception at 0x76de7a24 in tmpxXx.exe: 0xC0000005: Access violation reading location 0x00572120.
First-chance exception at 0x76de7a24 in tmpxXx.exe: 0xC0000005: Access violation reading location 0x00572120."

However, these exceptions does not cause a crash so I guess they are handled in DestroyWindow(), but I don't like there to be exceptions that I don't know where they are coming from.

I'm guessing that either DestroyWindow() is trying to free some memory that isn't allocated. Or maybe that when DestroyWindow() returns to a winproc for a window that no longer exist this causes the exceptions, but I find this very unlikely.

In my proper application I need to call the DestroyWindow() because windows should be able to be destroyed way before exiting the application, otherwise I could have just not bothered with DestroyWindow().

I cant see what I've done wrong. This example is barebone and it should be easy to find the problem.

Can anyone please shine a light on this? Where are these exceptions coming from and why? What should I do differently?

Thanks

Karl Hansson

 


Edit: New development

Thanks Guys!

Interestingly enough; if I comment out the WM_CLOSE case - then on the Win32 build the exceptions disappear.

But(!) on the x64 build the exceptions still happens just after the winproc returns from handeling the WM_DESTROY message.
(I forgot to mention before that I normally build in x64 rather than Win32)

This caused me to test whether having the WM_CLOSE case present in the Win32 build would work.
And it did, so to sum it up; on Win32 I don't get any exceptions but on x64 I do, with or without the WM_CLOSE and DestroyWindow().

This obviously leads me to think that it is something to do with my x64 builds rather than the DestroyWindow() it self.

Does anyone have a clue what is going on?

Thanks

Karl


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>