cleanup SDL_RegisterApp

- fix memory leak when RegisterClassEx fails
- set style according to the documentation
- eliminate duplicated SDL_Instance setter
This commit is contained in:
pionere 2022-01-17 09:58:16 +01:00 committed by Ryan C. Gordon
parent 0af391cbe9
commit 3c85cef46c

View file

@ -1579,6 +1579,14 @@ LPTSTR SDL_Appname = NULL;
Uint32 SDL_Appstyle = 0; Uint32 SDL_Appstyle = 0;
HINSTANCE SDL_Instance = NULL; 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 */ /* Register the class for this application */
int int
SDL_RegisterApp(const char *name, Uint32 style, void *hInst) SDL_RegisterApp(const char *name, Uint32 style, void *hInst)
@ -1592,19 +1600,16 @@ SDL_RegisterApp(const char *name, Uint32 style, void *hInst)
++app_registered; ++app_registered;
return (0); return (0);
} }
if (!name && !SDL_Appname) { SDL_assert(SDL_Appname == NULL);
if (!name) {
name = "SDL_app"; name = "SDL_app";
#if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC) #if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC); style = (CS_BYTEALIGNCLIENT | CS_OWNDC);
#endif #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 */ /* Register the application class */
wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbSize = sizeof(WNDCLASSEX);
@ -1635,6 +1640,7 @@ SDL_RegisterApp(const char *name, Uint32 style, void *hInst)
} }
if (!RegisterClassEx(&wcex)) { if (!RegisterClassEx(&wcex)) {
WIN_CleanRegisterApp(wcex);
return SDL_SetError("Couldn't register application class"); return SDL_SetError("Couldn't register application class");
} }
@ -1654,14 +1660,14 @@ SDL_UnregisterApp()
} }
--app_registered; --app_registered;
if (app_registered == 0) { if (app_registered == 0) {
/* Ensure the icons are initialized. */
wcex.hIcon = NULL;
wcex.hIconSm = NULL;
/* Check for any registered window classes. */ /* Check for any registered window classes. */
if (GetClassInfoEx(SDL_Instance, SDL_Appname, &wcex)) { if (GetClassInfoEx(SDL_Instance, SDL_Appname, &wcex)) {
UnregisterClass(SDL_Appname, SDL_Instance); UnregisterClass(SDL_Appname, SDL_Instance);
if (wcex.hIcon) DestroyIcon(wcex.hIcon);
if (wcex.hIconSm) DestroyIcon(wcex.hIconSm);
} }
SDL_free(SDL_Appname); WIN_CleanRegisterApp(wcex);
SDL_Appname = NULL;
} }
} }