cleanup SDL_RegisterApp - fix memory leak when RegisterClassEx fails - set style according to the documentation - eliminate duplicated SDL_Instance setter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index 844b913..ba25538 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/src/video/windows/SDL_windowsevents.c
@@ -1579,6 +1579,14 @@ LPTSTR SDL_Appname = NULL;
Uint32 SDL_Appstyle = 0;
HINSTANCE SDL_Instance = NULL;
+static void WIN_CleanRegisterApp(WNDCLASSEX wcex)
+{
+ if (wcex.hIcon) DestroyIcon(wcex.hIcon);
+ if (wcex.hIconSm) DestroyIcon(wcex.hIconSm);
+ SDL_free(SDL_Appname);
+ SDL_Appname = NULL;
+}
+
/* Register the class for this application */
int
SDL_RegisterApp(const char *name, Uint32 style, void *hInst)
@@ -1592,19 +1600,16 @@ SDL_RegisterApp(const char *name, Uint32 style, void *hInst)
++app_registered;
return (0);
}
- if (!name && !SDL_Appname) {
+ SDL_assert(SDL_Appname == NULL);
+ if (!name) {
name = "SDL_app";
#if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
- SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
+ style = (CS_BYTEALIGNCLIENT | CS_OWNDC);
#endif
- SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
- }
-
- if (name) {
- SDL_Appname = WIN_UTF8ToString(name);
- SDL_Appstyle = style;
- SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
}
+ SDL_Appname = WIN_UTF8ToString(name);
+ SDL_Appstyle = style;
+ SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
/* Register the application class */
wcex.cbSize = sizeof(WNDCLASSEX);
@@ -1635,6 +1640,7 @@ SDL_RegisterApp(const char *name, Uint32 style, void *hInst)
}
if (!RegisterClassEx(&wcex)) {
+ WIN_CleanRegisterApp(wcex);
return SDL_SetError("Couldn't register application class");
}
@@ -1654,14 +1660,14 @@ SDL_UnregisterApp()
}
--app_registered;
if (app_registered == 0) {
+ /* Ensure the icons are initialized. */
+ wcex.hIcon = NULL;
+ wcex.hIconSm = NULL;
/* Check for any registered window classes. */
if (GetClassInfoEx(SDL_Instance, SDL_Appname, &wcex)) {
UnregisterClass(SDL_Appname, SDL_Instance);
- if (wcex.hIcon) DestroyIcon(wcex.hIcon);
- if (wcex.hIconSm) DestroyIcon(wcex.hIconSm);
}
- SDL_free(SDL_Appname);
- SDL_Appname = NULL;
+ WIN_CleanRegisterApp(wcex);
}
}