From fa9210aa652ef50cf806ad11695a2aec682cb5f6 Mon Sep 17 00:00:00 2001 From: nhmall Date: Wed, 13 Nov 2024 13:48:50 -0500 Subject: [PATCH] Windows: free more allocated memory before exit This gets rid of the final leak complaint on Windows as of Nov 13, 2024 --- win/win32/NetHackW.c | 16 ++++++++++++++-- win/win32/mhmain.c | 4 +++- win/win32/mhmap.c | 2 ++ win/win32/mhmenu.c | 2 ++ win/win32/mhmsgwnd.c | 2 ++ win/win32/mhrip.c | 2 ++ win/win32/mhsplash.c | 2 ++ win/win32/mhstatus.c | 2 ++ win/win32/mhtext.c | 3 +++ win/win32/mswproc.c | 23 +++++++++++++---------- win/win32/winMS.h | 14 ++++++++++++-- 11 files changed, 57 insertions(+), 15 deletions(-) diff --git a/win/win32/NetHackW.c b/win/win32/NetHackW.c index 8a3144083..00a4d1b47 100644 --- a/win/win32/NetHackW.c +++ b/win/win32/NetHackW.c @@ -131,7 +131,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, windowprocs.win_wait_synch = mswin_wait_synch; win10_init(); - early_init(0, NULL); /* Change as needed to support CRASHREPORT */ + /* early_init(0, NULL); */ /* Change as needed to support CRASHREPORT */ /* init application structure */ _nethack_app.hApp = hInstance; @@ -166,6 +166,8 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, _nethack_app.bNoHScroll = FALSE; _nethack_app.bNoVScroll = FALSE; + if (_nethack_app.saved_text) + free(_nethack_app.saved_text), _nethack_app.saved_text = 0; _nethack_app.saved_text = strdup(""); _nethack_app.bAutoLayout = TRUE; @@ -257,7 +259,17 @@ free_winmain_stuff(void) for (cnt = 0; cnt < MAX_CMDLINE_PARAM; ++cnt) { if (argv[cnt]) - free((genericptr_t) argv[cnt]); + free((genericptr_t) argv[cnt]), argv[cnt] = 0; + } + if (_nethack_app.saved_text) + free((genericptr_t) _nethack_app.saved_text), + _nethack_app.saved_text = 0; + for (cnt = 0; cnt < MAXWINDOWS; ++cnt) { + if (windowdata[cnt].address) { + if (!windowdata[cnt].isstatic) + free(windowdata[cnt].address); + windowdata[cnt].address = 0; + } } } diff --git a/win/win32/mhmain.c b/win/win32/mhmain.c index bd1a5be79..959917cc6 100644 --- a/win/win32/mhmain.c +++ b/win/win32/mhmain.c @@ -208,9 +208,11 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) data = (PNHMainWindow) malloc(sizeof(NHMainWindow)); if (!data) panic("out of memory"); + ZeroMemory(data, sizeof(NHMainWindow)); data->mapAcsiiModeSave = MAP_MODE_ASCII12x16; SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); + windowdata[NHW_MAIN].address = (genericptr_t) data; /* update menu items */ CheckMenuItem( @@ -509,7 +511,7 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) /* clean up */ free((PNHMainWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA)); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); - + windowdata[NHW_MAIN].address = 0; // PostQuitMessage(0); exit(1); break; diff --git a/win/win32/mhmap.c b/win/win32/mhmap.c index 741cea720..6a5914cc6 100644 --- a/win/win32/mhmap.c +++ b/win/win32/mhmap.c @@ -641,6 +641,7 @@ MapWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) DeleteDC(data->backBufferDC); free(data); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); + windowdata[NHW_MAP].address = 0; break; case WM_TIMER: @@ -834,6 +835,7 @@ onCreate(HWND hWnd, WPARAM wParam, LPARAM lParam) ReleaseDC(hWnd, hDC); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); + windowdata[NHW_MAP].address = (genericptr_t) data; clearAll(data); diff --git a/win/win32/mhmenu.c b/win/win32/mhmenu.c index dfd7a49c9..41442cc69 100644 --- a/win/win32/mhmenu.c +++ b/win/win32/mhmenu.c @@ -298,6 +298,7 @@ MenuWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) data->bmpDC = CreateCompatibleDC(hdc); data->is_active = FALSE; SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); + windowdata[NHW_MENU].address = (genericptr_t) data; } /* set font for the text control */ cached_font * font = mswin_get_font(NHW_MENU, ATR_NONE, hdc, FALSE); @@ -519,6 +520,7 @@ MenuWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) } free(data); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); + windowdata[NHW_MENU].address = 0; } return TRUE; } diff --git a/win/win32/mhmsgwnd.c b/win/win32/mhmsgwnd.c index 2707facc3..864df34aa 100644 --- a/win/win32/mhmsgwnd.c +++ b/win/win32/mhmsgwnd.c @@ -171,6 +171,7 @@ NHMessageWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) data = (PNHMessageWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA); free(data); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); + windowdata[NHW_MESSAGE].address = 0; } break; case WM_SIZE: { @@ -729,6 +730,7 @@ onCreate(HWND hWnd, WPARAM wParam, LPARAM lParam) ZeroMemory(data, sizeof(NHMessageWindow)); data->max_text = MAXWINDOWTEXT; SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); + windowdata[NHW_MESSAGE].address = (genericptr_t) data; // for cleanup at the end /* re-calculate window size (+ font size) */ mswin_message_window_size(hWnd, &dummy); diff --git a/win/win32/mhrip.c b/win/win32/mhrip.c index 3fb9ac9ba..912bbd9db 100644 --- a/win/win32/mhrip.c +++ b/win/win32/mhrip.c @@ -56,6 +56,7 @@ mswin_init_RIP_window(void) ZeroMemory(data, sizeof(NHRIPWindow)); SetWindowLongPtr(ret, GWLP_USERDATA, (LONG_PTR) data); + windowdata[NHW_RIP].address = (genericptr_t) data; return ret; } @@ -240,6 +241,7 @@ NHRIPWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) DeleteObject(data->rip_bmp); free(data); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); + windowdata[NHW_RIP].address = 0; } break; } diff --git a/win/win32/mhsplash.c b/win/win32/mhsplash.c index 6d81bd17a..f928cbfc6 100644 --- a/win/win32/mhsplash.c +++ b/win/win32/mhsplash.c @@ -135,6 +135,8 @@ mswin_display_splash_window(BOOL show_ver) mswin_set_splash_data(hWnd, &splashData, monitorInfo.scale); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) &splashData); + windowdata[NHW_SPLASH].address = (genericptr_t) &splashData; + windowdata[NHW_SPLASH].isstatic = 1; GetNHApp()->hPopupWnd = hWnd; diff --git a/win/win32/mhstatus.c b/win/win32/mhstatus.c index d96992c93..fffbc5100 100644 --- a/win/win32/mhstatus.c +++ b/win/win32/mhstatus.c @@ -127,6 +127,7 @@ mswin_init_status_window(void) if (!data) panic("out of memory"); + windowdata[NHW_STATUS].address = (genericptr_t) data; ZeroMemory(data, sizeof(NHStatusWindow)); SetWindowLongPtr(ret, GWLP_USERDATA, (LONG_PTR) data); @@ -253,6 +254,7 @@ StatusWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_DESTROY: free(data); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); + windowdata[NHW_STATUS].address = 0; break; case WM_SETFOCUS: diff --git a/win/win32/mhtext.c b/win/win32/mhtext.c index c51b50eb1..8463a187e 100644 --- a/win/win32/mhtext.c +++ b/win/win32/mhtext.c @@ -83,8 +83,10 @@ NHTextWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) data = (PNHTextWindow)malloc(sizeof(NHTextWindow)); if (!data) panic("out of memory"); + ZeroMemory(data, sizeof(NHTextWindow)); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)data); + windowdata[NHW_TEXT].address = (genericptr_t) data; // for cleanup at the end HWND control = GetDlgItem(hWnd, IDC_TEXT_CONTROL); HDC hdc = GetDC(control); @@ -173,6 +175,7 @@ NHTextWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) free(data->window_text); free(data); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); + windowdata[NHW_TEXT].address = 0; } break; diff --git a/win/win32/mswproc.c b/win/win32/mswproc.c index 40bfeb7ba..42abf9a19 100644 --- a/win/win32/mswproc.c +++ b/win/win32/mswproc.c @@ -1021,6 +1021,7 @@ mswin_putstr_ex(winid wid, int attr, const char *text, int app) if (GetNHApp()->windowlist[wid].win != NULL) { MSNHMsgPutstr data; + ZeroMemory(&data, sizeof(data)); data.attr = attr; data.text = text; @@ -2110,30 +2111,32 @@ mswin_preference_update(const char *pref) } } +static PMSNHMsgGetText history_text = 0; +static char *next_message = 0; + char * mswin_getmsghistory(boolean init) { - static PMSNHMsgGetText text = 0; - static char *next_message = 0; - if (init) { - text = (PMSNHMsgGetText) malloc(sizeof(MSNHMsgGetText) + if (history_text) + free((genericptr_t) history_text), history_text = 0; + history_text = (PMSNHMsgGetText) malloc(sizeof(MSNHMsgGetText) + TEXT_BUFFER_SIZE); - if (text) { - text->max_size = + if (history_text) { + history_text->max_size = TEXT_BUFFER_SIZE - 1; /* make sure we always have 0 at the end of the buffer */ - ZeroMemory(text->buffer, TEXT_BUFFER_SIZE); + ZeroMemory(history_text->buffer, TEXT_BUFFER_SIZE); SendMessage(mswin_hwnd_from_winid(WIN_MESSAGE), WM_MSNH_COMMAND, - (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text); + (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) history_text); - next_message = text->buffer; + next_message = history_text->buffer; } } if (!(next_message && next_message[0])) { - free(text); + free(history_text), history_text = 0; next_message = 0; return (char *) 0; } else { diff --git a/win/win32/winMS.h b/win/win32/winMS.h index 9e92d92ec..6b0170e91 100644 --- a/win/win32/winMS.h +++ b/win/win32/winMS.h @@ -43,8 +43,18 @@ #define MAXWINDOWS 15 #endif -#define NHW_RIP 32 -#define NHW_INVEN 33 +struct window_tracking_data { + genericptr_t address; + int isstatic; +} windowdata[MAXWINDOWS]; + +/* these are only in MSWIN_GRAPHICS, not the core */ +enum mswin_window_types { + NHW_MAIN = (NHW_LAST_TYPE + 1), + NHW_INVEN, + NHW_RIP, + NHW_SPLASH +}; #ifndef TILE_X #define TILE_X 16