Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Exception Handling #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/dwm-win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,14 @@ forcearrange(const Arg *arg) {
arrange();
}

//catches unhandled exceptions and performs necessary cleanup before a crash, you can use Structured Exception Handling (SEH) in Windows
// This function will be called when an exception occurs
void cleanupOnException() {
Client *c;
for (c = clients; c; c = c->next) {
unmanage(c);
}
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd) {
SetProcessDPIAware();
Expand All @@ -1662,14 +1670,23 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
die(L"dwm-win32 already running");

lua_State *L = luaL_newstate();
setup(L, hInstance);

// the __try block contains the main program code. If an unhandled exception occurs anywhere in this block, control immediately transfers to the __except block. The cleanupOnException function is then called to unmanage all windows before the program crashes.
// Establish an exception handler
__try {
setup(L, hInstance);

while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
cleanup(L);
}
// Catch any unhandled exceptions
__except(EXCEPTION_EXECUTE_HANDLER) {
cleanupOnException();
}

cleanup(L);

return msg.wParam;
}