www.digitalmars.com         C & C++   DMDScript  

D - Windows app crashes on exit

reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Using DMD 0.77, Windows 98SE.

I'm experimenting with writing a Windows application framework rather in 
the style of Borland OWL.  The code below is a simplification of this.

The base version (i.e. no -version compiler switch) runs without any 
trouble.

Version 1 is, of course, supposed to make multiple windows (and, of 
course, dialogs) possible.  It still manages to do nothing without any 
trouble.  However, as soon as I close it, I get an illegal operation error:

WINTEST caused an invalid page fault in
module KERNEL32.DLL at 0167:bff8ac13.

I can't for the life of me figure why this is happening.  What is going 
on here?  Any idea how to fix it?

Stewart.

----------

import std.c.windows.windows;

version (1) {
	Window[HWND] windowList;
	Window newWindow;

	extern(Windows)
	int WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam) {
		Window w;

		if (!(hWnd in windowList)) {
			windowList[hWnd] = w = newWindow;
			newWindow = null;
		} else {
			w = windowList[hWnd];
		}

		return w.processMessage(hWnd, uMsg, wParam, lParam);
	}
} else {
	Window w;

	extern(Windows)
	int WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam) {
		return w.processMessage(hWnd, uMsg, wParam, lParam);
	}
}

class Window {

	int processMessage(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam) {
		switch (uMsg) {
			case WM_DESTROY:
				PostQuitMessage(0);
				break;

			default:
				break;
		}
		return DefWindowProcA(hWnd, uMsg, wParam, lParam);
	}

	int run() {
		HINSTANCE hInst = GetModuleHandleA(null);

		WNDCLASS wc;
		wc.lpszClassName = "DWndClass";
		wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
		wc.lpfnWndProc = &WindowProc;
		wc.hInstance = hInst;
		wc.hIcon = LoadIconA(cast(HINSTANCE) null, IDI_APPLICATION);
		wc.hCursor = LoadCursorA(cast(HINSTANCE) null, cast(LPSTR) 32512);
		wc.hbrBackground = cast(HBRUSH) (COLOR_WINDOW + 1);
		wc.lpszMenuName = null;
		wc.cbClsExtra = wc.cbWndExtra = 0;

		assert(RegisterClassA(&wc));

		HWND hWnd;

		version(1) {
			synchronized {
				newWindow = this;

				hWnd = CreateWindowA("DWndClass", "Just a window", WS_THICKFRAME |
					WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE,
					CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, HWND_DESKTOP,
					cast(HMENU) null, hInst, null);
			}

		} else {
			hWnd = CreateWindowA("DWndClass", "Just a window", WS_THICKFRAME |
				WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE,
				CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, HWND_DESKTOP,
				cast(HMENU) null, hInst, null);
		}
		assert(hWnd);

		MSG msg;
		while (GetMessageA(&msg, cast(HWND) null, 0, 0)) {
			TranslateMessage(&msg);
			DispatchMessageA(&msg);
		}

		return 1;
	}

}

int doit() {
	version (1) {
		Window w;
	}
	w = new Window;
	return w.run();
}

extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleUnitTests();

extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	  LPSTR lpCmdLine, int nCmdShow) {
     int result;

     gc_init();			// initialize garbage collector
     _minit();			// initialize module constructor table

     try {
		_moduleCtor();		// call module constructors
		_moduleUnitTests();	// run unit tests (optional)

		result = doit();	// insert user code here
     }

     catch (Object o) {		// catch any uncaught exceptions
		MessageBoxA(null, (char *)o.toString(), "Runtime Error",
				MB_OK | MB_ICONEXCLAMATION);
		result = 0;		// failed
     }

     gc_term();			// run finalizers; terminate garbage collector
     return result;
}

-- 
My e-mail is valid but not my primary mailbox, aside from its being the 
unfortunate victim of intensive mail-bombing at the moment.  Please keep 
replies on the 'group where everyone may benefit.
Jan 14 2004
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
While it was 14/1/04 1:30 pm throughout the UK, Stewart Gordon sprinkled 
little black dots on a white screen, and they fell thus:

 Using DMD 0.77, Windows 98SE.
<snip>
 WINTEST caused an invalid page fault in
 module KERNEL32.DLL at 0167:bff8ac13.
 
 I can't for the life of me figure why this is happening.  What is going 
 on here?  Any idea how to fix it?
<snip> Well if nobody's found the solution, it seems I've found it. Just upgraded to DMD 0.78 and it seems to work now. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Jan 15 2004
parent reply yaneurao <yaneurao_member pathlink.com> writes:
In article <bu5v03$1qkr$1 digitaldaemon.com>, Stewart Gordon says...
 Using DMD 0.77, Windows 98SE.
<snip>
 WINTEST caused an invalid page fault in
 module KERNEL32.DLL at 0167:bff8ac13.
Well if nobody's found the solution, it seems I've found it. Just upgraded to DMD 0.78 and it seems to work now.
I had reported it and fixed in DMD 0.78. D/21167 yaneurao.
Jan 15 2004
parent "Walter" <walter digitalmars.com> writes:
"yaneurao" <yaneurao_member pathlink.com> wrote in message
news:bu69fq$2b6s$1 digitaldaemon.com...
 I had reported it and fixed in DMD 0.78.
 D/21167
Yes, thanks to you!
Jan 15 2004