release some memory before exit under Windows

This commit is contained in:
nhmall
2024-11-11 16:50:57 -05:00
parent fd0b67de4d
commit 7414b906b2
4 changed files with 35 additions and 17 deletions

View File

@@ -1128,8 +1128,6 @@ consoletty_open(int mode)
void void
consoletty_exit(void) consoletty_exit(void)
{ {
/* frees some status tracking data */
genl_status_finish();
/* go back to using the safe routines */ /* go back to using the safe routines */
safe_routines(); safe_routines();
free_custom_colors(); free_custom_colors();

View File

@@ -499,8 +499,9 @@ extern const char *known_restrictions[]; /* symbols.c */
DISABLE_WARNING_UNREACHABLE_CODE DISABLE_WARNING_UNREACHABLE_CODE
#if defined(__MINGW32__) && defined(MSWIN_GRAPHICS)
#define MAIN mingw_main #if defined(MSWIN_GRAPHICS)
#define MAIN nethackw_main
#else #else
#define MAIN main #define MAIN main
#endif #endif

View File

@@ -483,6 +483,10 @@ get_port_id(char *buf)
} }
#endif /* RUNTIME_PORT_ID */ #endif /* RUNTIME_PORT_ID */
#ifdef MSWIN_GRAPHICS
extern void free_winmain_stuff(void);
#endif
void void
nethack_exit(int code) nethack_exit(int code)
{ {
@@ -506,6 +510,11 @@ nethack_exit(int code)
if (iflags.window_inited) if (iflags.window_inited)
wait_synch(); wait_synch();
} }
/* frees some status tracking data */
genl_status_finish();
#ifdef MSWIN_GRAPHICS
free_winmain_stuff();
#endif
exit(code); exit(code);
} }

View File

@@ -82,13 +82,16 @@ static void __cdecl mswin_moveloop(void *);
#pragma warning( disable : 28251) #pragma warning( disable : 28251)
#endif #endif
extern int nethackw_main(int argc, char *argv[]);
static char *argv[MAX_CMDLINE_PARAM];
int APIENTRY int APIENTRY
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow) int nCmdShow)
{ {
INITCOMMONCONTROLSEX InitCtrls; INITCOMMONCONTROLSEX InitCtrls;
int argc; int argc;
char *argv[MAX_CMDLINE_PARAM];
size_t len; size_t len;
TCHAR *p; TCHAR *p;
TCHAR wbuf[BUFSZ]; TCHAR wbuf[BUFSZ];
@@ -210,14 +213,14 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
for (argc = 1; p && argc < MAX_CMDLINE_PARAM; argc++) { for (argc = 1; p && argc < MAX_CMDLINE_PARAM; argc++) {
len = _tcslen(p); len = _tcslen(p);
if (len > 0) { if (len > 0) {
argv[argc] = _strdup(NH_W2A(p, buf, BUFSZ)); argv[argc] = strdup(NH_W2A(p, buf, BUFSZ));
} else { } else {
argv[argc] = ""; argv[argc] = strdup("");
} }
p = _get_cmd_arg(NULL); p = _get_cmd_arg(NULL);
} }
GetModuleFileName(NULL, wbuf, BUFSZ); GetModuleFileName(NULL, wbuf, BUFSZ);
argv[0] = _strdup(NH_W2A(wbuf, buf, BUFSZ)); argv[0] = strdup(NH_W2A(wbuf, buf, BUFSZ));
if (argc == 2) { if (argc == 2) {
TCHAR *savefile = strdup(argv[1]); TCHAR *savefile = strdup(argv[1]);
@@ -232,8 +235,8 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
if (*p) { if (*p) {
if (strcmp(p + 1, "NetHack-saved-game") == 0) { if (strcmp(p + 1, "NetHack-saved-game") == 0) {
*p = '\0'; *p = '\0';
argv[1] = "-u"; argv[1] = strdup("-u");
argv[2] = _strdup(name); argv[2] = strdup(name);
argc = 3; argc = 3;
} }
} }
@@ -241,16 +244,23 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
free(savefile); free(savefile);
} }
GUILaunched = 1; GUILaunched = 1;
/* let main do the argument processing */ /* let nethackw_main do the argument processing */
#ifndef __MINGW32__ nethackw_main(argc, argv);
main(argc, argv); /* not reached */
#else
int mingw_main(int argc, char *argv[]);
mingw_main(argc, argv);
#endif
return 0; return 0;
} }
void
free_winmain_stuff(void)
{
int cnt;
for (cnt = 0; cnt < MAX_CMDLINE_PARAM; ++cnt) {
if (argv[cnt] && argv)
free((genericptr_t) argv[cnt]);
}
}
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning( pop ) #pragma warning( pop )
#endif #endif