Windows: free more allocated memory before exit

This gets rid of the final leak complaint on Windows as of Nov 13, 2024
This commit is contained in:
nhmall
2024-11-13 13:48:50 -05:00
parent c85b5d3e56
commit fa9210aa65
11 changed files with 57 additions and 15 deletions

View File

@@ -131,7 +131,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
windowprocs.win_wait_synch = mswin_wait_synch; windowprocs.win_wait_synch = mswin_wait_synch;
win10_init(); 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 */ /* init application structure */
_nethack_app.hApp = hInstance; _nethack_app.hApp = hInstance;
@@ -166,6 +166,8 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
_nethack_app.bNoHScroll = FALSE; _nethack_app.bNoHScroll = FALSE;
_nethack_app.bNoVScroll = 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.saved_text = strdup("");
_nethack_app.bAutoLayout = TRUE; _nethack_app.bAutoLayout = TRUE;
@@ -257,7 +259,17 @@ free_winmain_stuff(void)
for (cnt = 0; cnt < MAX_CMDLINE_PARAM; ++cnt) { for (cnt = 0; cnt < MAX_CMDLINE_PARAM; ++cnt) {
if (argv[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;
}
} }
} }

View File

@@ -208,9 +208,11 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
data = (PNHMainWindow) malloc(sizeof(NHMainWindow)); data = (PNHMainWindow) malloc(sizeof(NHMainWindow));
if (!data) if (!data)
panic("out of memory"); panic("out of memory");
ZeroMemory(data, sizeof(NHMainWindow)); ZeroMemory(data, sizeof(NHMainWindow));
data->mapAcsiiModeSave = MAP_MODE_ASCII12x16; data->mapAcsiiModeSave = MAP_MODE_ASCII12x16;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data);
windowdata[NHW_MAIN].address = (genericptr_t) data;
/* update menu items */ /* update menu items */
CheckMenuItem( CheckMenuItem(
@@ -509,7 +511,7 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
/* clean up */ /* clean up */
free((PNHMainWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA)); free((PNHMainWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA));
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0);
windowdata[NHW_MAIN].address = 0;
// PostQuitMessage(0); // PostQuitMessage(0);
exit(1); exit(1);
break; break;

View File

@@ -641,6 +641,7 @@ MapWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
DeleteDC(data->backBufferDC); DeleteDC(data->backBufferDC);
free(data); free(data);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0);
windowdata[NHW_MAP].address = 0;
break; break;
case WM_TIMER: case WM_TIMER:
@@ -834,6 +835,7 @@ onCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
ReleaseDC(hWnd, hDC); ReleaseDC(hWnd, hDC);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data);
windowdata[NHW_MAP].address = (genericptr_t) data;
clearAll(data); clearAll(data);

View File

@@ -298,6 +298,7 @@ MenuWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
data->bmpDC = CreateCompatibleDC(hdc); data->bmpDC = CreateCompatibleDC(hdc);
data->is_active = FALSE; data->is_active = FALSE;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data);
windowdata[NHW_MENU].address = (genericptr_t) data;
} }
/* set font for the text control */ /* set font for the text control */
cached_font * font = mswin_get_font(NHW_MENU, ATR_NONE, hdc, FALSE); 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); free(data);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0);
windowdata[NHW_MENU].address = 0;
} }
return TRUE; return TRUE;
} }

View File

@@ -171,6 +171,7 @@ NHMessageWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
data = (PNHMessageWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA); data = (PNHMessageWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA);
free(data); free(data);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0);
windowdata[NHW_MESSAGE].address = 0;
} break; } break;
case WM_SIZE: { case WM_SIZE: {
@@ -729,6 +730,7 @@ onCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
ZeroMemory(data, sizeof(NHMessageWindow)); ZeroMemory(data, sizeof(NHMessageWindow));
data->max_text = MAXWINDOWTEXT; data->max_text = MAXWINDOWTEXT;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); 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) */ /* re-calculate window size (+ font size) */
mswin_message_window_size(hWnd, &dummy); mswin_message_window_size(hWnd, &dummy);

View File

@@ -56,6 +56,7 @@ mswin_init_RIP_window(void)
ZeroMemory(data, sizeof(NHRIPWindow)); ZeroMemory(data, sizeof(NHRIPWindow));
SetWindowLongPtr(ret, GWLP_USERDATA, (LONG_PTR) data); SetWindowLongPtr(ret, GWLP_USERDATA, (LONG_PTR) data);
windowdata[NHW_RIP].address = (genericptr_t) data;
return ret; return ret;
} }
@@ -240,6 +241,7 @@ NHRIPWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
DeleteObject(data->rip_bmp); DeleteObject(data->rip_bmp);
free(data); free(data);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0);
windowdata[NHW_RIP].address = 0;
} }
break; break;
} }

View File

@@ -135,6 +135,8 @@ mswin_display_splash_window(BOOL show_ver)
mswin_set_splash_data(hWnd, &splashData, monitorInfo.scale); mswin_set_splash_data(hWnd, &splashData, monitorInfo.scale);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) &splashData); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) &splashData);
windowdata[NHW_SPLASH].address = (genericptr_t) &splashData;
windowdata[NHW_SPLASH].isstatic = 1;
GetNHApp()->hPopupWnd = hWnd; GetNHApp()->hPopupWnd = hWnd;

View File

@@ -127,6 +127,7 @@ mswin_init_status_window(void)
if (!data) if (!data)
panic("out of memory"); panic("out of memory");
windowdata[NHW_STATUS].address = (genericptr_t) data;
ZeroMemory(data, sizeof(NHStatusWindow)); ZeroMemory(data, sizeof(NHStatusWindow));
SetWindowLongPtr(ret, GWLP_USERDATA, (LONG_PTR) data); SetWindowLongPtr(ret, GWLP_USERDATA, (LONG_PTR) data);
@@ -253,6 +254,7 @@ StatusWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_DESTROY: case WM_DESTROY:
free(data); free(data);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0);
windowdata[NHW_STATUS].address = 0;
break; break;
case WM_SETFOCUS: case WM_SETFOCUS:

View File

@@ -83,8 +83,10 @@ NHTextWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
data = (PNHTextWindow)malloc(sizeof(NHTextWindow)); data = (PNHTextWindow)malloc(sizeof(NHTextWindow));
if (!data) if (!data)
panic("out of memory"); panic("out of memory");
ZeroMemory(data, sizeof(NHTextWindow)); ZeroMemory(data, sizeof(NHTextWindow));
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)data); 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); HWND control = GetDlgItem(hWnd, IDC_TEXT_CONTROL);
HDC hdc = GetDC(control); HDC hdc = GetDC(control);
@@ -173,6 +175,7 @@ NHTextWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
free(data->window_text); free(data->window_text);
free(data); free(data);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0);
windowdata[NHW_TEXT].address = 0;
} }
break; break;

View File

@@ -1021,6 +1021,7 @@ mswin_putstr_ex(winid wid, int attr, const char *text, int app)
if (GetNHApp()->windowlist[wid].win != NULL) { if (GetNHApp()->windowlist[wid].win != NULL) {
MSNHMsgPutstr data; MSNHMsgPutstr data;
ZeroMemory(&data, sizeof(data)); ZeroMemory(&data, sizeof(data));
data.attr = attr; data.attr = attr;
data.text = text; data.text = text;
@@ -2110,30 +2111,32 @@ mswin_preference_update(const char *pref)
} }
} }
static PMSNHMsgGetText history_text = 0;
static char *next_message = 0;
char * char *
mswin_getmsghistory(boolean init) mswin_getmsghistory(boolean init)
{ {
static PMSNHMsgGetText text = 0;
static char *next_message = 0;
if (init) { 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); + TEXT_BUFFER_SIZE);
if (text) { if (history_text) {
text->max_size = history_text->max_size =
TEXT_BUFFER_SIZE TEXT_BUFFER_SIZE
- 1; /* make sure we always have 0 at the end of the buffer */ - 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, 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])) { if (!(next_message && next_message[0])) {
free(text); free(history_text), history_text = 0;
next_message = 0; next_message = 0;
return (char *) 0; return (char *) 0;
} else { } else {

View File

@@ -43,8 +43,18 @@
#define MAXWINDOWS 15 #define MAXWINDOWS 15
#endif #endif
#define NHW_RIP 32 struct window_tracking_data {
#define NHW_INVEN 33 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 #ifndef TILE_X
#define TILE_X 16 #define TILE_X 16