1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Close all thread handles returned from CreateThread.

If you don't, they are permanently leaked. A user points out that this
is particularly bad in Pageant, with the new named-pipe-based IPC,
since it will spawn an input and output I/O thread per named pipe
connection, leading to two handles being leaked every time.

(cherry picked from commit c714dfc936)
This commit is contained in:
Simon Tatham 2021-07-03 11:01:09 +01:00
parent 22d7888b33
commit ea45d7dcd8
4 changed files with 20 additions and 10 deletions

View File

@ -5440,8 +5440,10 @@ static void wintw_clip_request_paste(TermWin *tw, int clipboard)
* that tells us it's OK to paste.
*/
DWORD in_threadid; /* required for Win9x */
CreateThread(NULL, 0, clipboard_read_threadfunc,
wgs.term_hwnd, 0, &in_threadid);
HANDLE hThread = CreateThread(NULL, 0, clipboard_read_threadfunc,
wgs.term_hwnd, 0, &in_threadid);
if (hThread)
CloseHandle(hThread); /* we don't need the thread handle */
}
/*

View File

@ -451,8 +451,10 @@ struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata,
handles_by_evtomain = newtree234(handle_cmp_evtomain);
add234(handles_by_evtomain, h);
CreateThread(NULL, 0, handle_input_threadfunc,
&h->u.i, 0, &in_threadid);
HANDLE hThread = CreateThread(NULL, 0, handle_input_threadfunc,
&h->u.i, 0, &in_threadid);
if (hThread)
CloseHandle(hThread); /* we don't need the thread handle */
h->u.i.busy = true;
return h;
@ -482,8 +484,10 @@ struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata,
handles_by_evtomain = newtree234(handle_cmp_evtomain);
add234(handles_by_evtomain, h);
CreateThread(NULL, 0, handle_output_threadfunc,
&h->u.o, 0, &out_threadid);
HANDLE hThread = CreateThread(NULL, 0, handle_output_threadfunc,
&h->u.o, 0, &out_threadid);
if (hThread)
CloseHandle(hThread); /* we don't need the thread handle */
return h;
}

View File

@ -1160,13 +1160,15 @@ static void start_generating_key(HWND hwnd, struct MainDlgState *state)
params->key = &state->key;
params->dsskey = &state->dsskey;
if (!CreateThread(NULL, 0, generate_key_thread,
params, 0, &threadid)) {
HANDLE hThread = CreateThread(NULL, 0, generate_key_thread,
params, 0, &threadid);
if (!hThread) {
MessageBox(hwnd, "Out of thread resources",
"Key generation error",
MB_OK | MB_ICONERROR);
sfree(params);
} else {
CloseHandle(hThread); /* we don't need the thread handle */
state->generation_thread_exists = true;
}
}

View File

@ -1656,8 +1656,10 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
DWORD wm_copydata_threadid;
wmct.ev_msg_ready = CreateEvent(NULL, false, false, NULL);
wmct.ev_reply_ready = CreateEvent(NULL, false, false, NULL);
CreateThread(NULL, 0, wm_copydata_threadfunc,
&inst, 0, &wm_copydata_threadid);
HANDLE hThread = CreateThread(NULL, 0, wm_copydata_threadfunc,
&inst, 0, &wm_copydata_threadid);
if (hThread)
CloseHandle(hThread); /* we don't need the thread handle */
handle_add_foreign_event(wmct.ev_msg_ready, wm_copydata_got_msg, NULL);
if (show_keylist_on_startup)