[ttssh2-commit] [10534] シリアル設定を行うとメモリリークが発生していたので修正

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2023年 1月 25日 (水) 22:53:40 JST


Revision: 10534
          https://osdn.net/projects/ttssh2/scm/svn/commits/10534
Author:   zmatsuo
Date:     2023-01-25 22:53:40 +0900 (Wed, 25 Jan 2023)
Log Message:
-----------
シリアル設定を行うとメモリリークが発生していたので修正

- DetectComPorts() から ComPortInfoGet() を使用するようにした
  - DetectComPorts() を使用しているのはテストプログラム(lscom)だけになった
  - DetectComPorts() はexportされた関数なので削除せずに残しておく
- DetectComPorts() 内部から ComPortInfoGet() をコールしていた
  - r9197 で変更
- DetectComPorts() 内で確保したメモリは開放していなかった
  - 再度 DetectComPorts() をコールしたときに解放されていた
  - 従来からの動作

Revision Links:
--------------
    https://osdn.net/projects/ttssh2/scm/svn/commits/9197

Modified Paths:
--------------
    trunk/teraterm/ttpdlg/ttdlg.c

-------------- next part --------------
Modified: trunk/teraterm/ttpdlg/ttdlg.c
===================================================================
--- trunk/teraterm/ttpdlg/ttdlg.c	2023-01-24 14:25:46 UTC (rev 10533)
+++ trunk/teraterm/ttpdlg/ttdlg.c	2023-01-25 13:53:40 UTC (rev 10534)
@@ -131,12 +131,6 @@
 	SendDlgItemMessageA(HDlg, id, CB_SETCURSEL, sel_index, 0);
 }
 
-/*
- * COM\x83|\x81[\x83g\x82Ɋւ\xB7\x82\xE9\x8Fڍ׏\xEE\x95\xF1
- */
-static ComPortInfo_t *ComPortInfoPtr;
-static int ComPortInfoCount;
-
 static INT_PTR CALLBACK TermDlg(HWND Dialog, UINT Message, WPARAM wParam, LPARAM lParam)
 {
 	static const DlgTextInfo TextInfosCom[] = {
@@ -991,6 +985,12 @@
 	return FALSE;
 }
 
+typedef struct {
+	PTTSet pts;
+	ComPortInfo_t *ComPortInfoPtr;
+	int ComPortInfoCount;
+} SerialDlgData;
+
 static const char *DataList[] = {"7 bit","8 bit",NULL};
 static const char *ParityList[] = {"none", "odd", "even", "mark", "space", NULL};
 static const char *StopList[] = {"1 bit", "2 bit", NULL};
@@ -1053,20 +1053,15 @@
  * \x83V\x83\x8A\x83A\x83\x8B\x83|\x81[\x83g\x90ݒ\xE8\x83_\x83C\x83A\x83\x8D\x83O\x82̃e\x83L\x83X\x83g\x83{\x83b\x83N\x83X\x82\xC9COM\x83|\x81[\x83g\x82̏ڍ׏\xEE\x95\xF1\x82\xF0\x95\\x8E\xA6\x82\xB7\x82\xE9\x81B
  *
  */
-static void serial_dlg_set_comport_info(HWND dlg, int portno, char *desc)
+static void serial_dlg_set_comport_info(HWND dlg, SerialDlgData *dlg_data, int port_index)
 {
-	int i;
-
-	for (i = 0; i < ComPortInfoCount; i++) {
-		const ComPortInfo_t *p = &ComPortInfoPtr[i];
-		if (p->port_no == portno) {
-			SetDlgItemTextW(dlg, IDC_SERIALTEXT, p->property);
-			return;
-		}
+	if (port_index + 1 > dlg_data->ComPortInfoCount) {
+		SetDlgItemTextW(dlg, IDC_SERIALTEXT, NULL);
 	}
-
-	// \x8AY\x93\x96\x82\xB7\x82\xE9COM\x83|\x81[\x83g\x82\xAA\x8C\xA9\x82‚\xA9\x82\xE7\x82Ȃ\xA9\x82\xC1\x82\xBD
-	SetDlgItemTextW(dlg, IDC_SERIALTEXT, NULL);
+	else {
+		const ComPortInfo_t *p = &dlg_data->ComPortInfoPtr[port_index];
+		SetDlgItemTextW(dlg, IDC_SERIALTEXT, p->property);
+	}
 }
 
 /*
@@ -1182,19 +1177,16 @@
 		{ IDCANCEL, "BTN_CANCEL" },
 		{ IDC_SERIALHELP, "BTN_HELP" },
 	};
-	PTTSet ts = (PTTSet)GetWindowLongPtr(Dialog, DWLP_USER);
+	SerialDlgData *dlg_data = (SerialDlgData *)GetWindowLongPtr(Dialog, DWLP_USER);
+	PTTSet ts = dlg_data == NULL ? NULL : dlg_data->pts;
 	int i, w, sel;
-	char Temp[128];
-	static WORD ComPortTable[MAXCOMPORT];  // \x8Eg\x97p\x89”\\x82\xC8COM\x83|\x81[\x83g\x94ԍ\x86
-	static char *ComPortDesc[MAXCOMPORT];  // COM\x83|\x81[\x83g\x82̏ڍ׏\xEE\x95\xF1
-	static int comports; // \x83e\x81[\x83u\x83\x8B\x8Dő吔
 	WORD Flow;
-	int portno;
 
 	switch (Message) {
 		case WM_INITDIALOG:
-			ts = (PTTSet)lParam;
+			dlg_data = (SerialDlgData *)lParam;
 			SetWindowLongPtr(Dialog, DWLP_USER, lParam);
+			ts = dlg_data->pts;
 
 			SetDlgTextsW(Dialog, TextInfos, _countof(TextInfos), ts->UILanguageFileW);
 
@@ -1202,51 +1194,33 @@
 			EnableDlgItem(Dialog, IDC_SERIALPORT_LABEL, IDC_SERIALPORT_LABEL);
 			EnableDlgItem(Dialog, IDOK, IDOK);
 
-			// COM\x83|\x81[\x83g\x82̏ڍ׏\xEE\x95\xF1\x82\xF0\x8E擾\x82\xB7\x82\xE9\x81B
-			// COM\x82̐ڑ\xB1\x8F󋵂͓s\x93x\x95ς\xED\x82邽\x82߁A\x83_\x83C\x83A\x83\x8D\x83O\x82\xF0\x95\\x8E\xA6\x82\xB7\x82\xE9\x93x\x82Ɏ擾\x82\xB7\x82\xE9\x81B
-			// \x95s\x97v\x82ɂȂ\xC1\x82\xBD\x82\xE7\x81AComPortInfoFree()\x82Ń\x81\x83\x82\x83\x8A\x82\xF0\x89\xF0\x95\xFA\x82\xB7\x82邱\x82ƁB
-			ComPortInfoPtr = ComPortInfoGet(&ComPortInfoCount);
-
 			w = 0;
 
-			if ((comports = DetectComPorts(ComPortTable, ts->MaxComPort, ComPortDesc)) > 0) {
-				for (i=0; i<comports; i++) {
+			if (dlg_data->ComPortInfoCount > 0) {
+				for (i = 0; i < dlg_data->ComPortInfoCount; i++) {
+					ComPortInfo_t *p = dlg_data->ComPortInfoPtr + i;
+					wchar_t *EntNameW;
+
 					// MaxComPort \x82\xF0\x89z\x82\xA6\x82\xE9\x83|\x81[\x83g\x82͕\\x8E\xA6\x82\xB5\x82Ȃ\xA2
-					if (ComPortTable[i] > ts->MaxComPort) {
+					if (i > ts->MaxComPort) {
 						continue;
 					}
 
-					_snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "COM%d", ComPortTable[i]);
-// Serial dialog\x82̓h\x83\x8D\x83b\x83v\x83_\x83E\x83\x93\x83\x8A\x83X\x83g\x82̕\x9D\x82\xAA\x91傫\x82\xAD\x82ł\xAB\x82Ȃ\xA2\x82̂ŁADescription\x82͂Ȃ\xB5\x82Ƃ\xB7\x82\xE9\x81B
-#if 0
-					strncat_s(Temp, sizeof(Temp), ": ", _TRUNCATE);
-					strncat_s(Temp, sizeof(Temp), ComPortDesc[i], _TRUNCATE);
-#endif
-					SendDlgItemMessage(Dialog, IDC_SERIALPORT, CB_ADDSTRING,
-					                   0, (LPARAM)Temp);
-					if (ComPortTable[i] == ts->ComPort) {
+					aswprintf(&EntNameW, L"%s", p->port_name);
+					SendDlgItemMessageW(Dialog, IDC_SERIALPORT, CB_ADDSTRING, 0, (LPARAM)EntNameW);
+					free(EntNameW);
+
+					if (p->port_no == ts->ComPort) {
 						w = i;
 					}
-
-					// \x8Fڍ׏\xEE\x95\xF1\x82\xF0\x95\\x8E\xA6\x82\xB7\x82\xE9
-					serial_dlg_set_comport_info(Dialog, ComPortTable[w], ComPortDesc[w]);
-
 				}
-			} else if (comports == 0) {
+				serial_dlg_set_comport_info(Dialog, dlg_data, w);
+			}
+			else { //if (ComPortInfoCount == 0) {
 				DisableDlgItem(Dialog, IDC_SERIALPORT, IDC_SERIALPORT);
 				DisableDlgItem(Dialog, IDC_SERIALPORT_LABEL, IDC_SERIALPORT_LABEL);
 				// COM\x83|\x81[\x83g\x82\xAA\x91\xB6\x8D݂\xB5\x82Ȃ\xA2\x8Fꍇ\x82\xCDOK\x83{\x83^\x83\x93\x82\xF0\x89\x9F\x82\xB9\x82Ȃ\xA2\x82悤\x82ɂ\xB7\x82\xE9\x81B
 				DisableDlgItem(Dialog, IDOK, IDOK);
-			} else {
-				for (i=1; i<=ts->MaxComPort; i++) {
-					_snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "COM%d", i);
-					SendDlgItemMessage(Dialog, IDC_SERIALPORT, CB_ADDSTRING,
-					                   0, (LPARAM)Temp);
-				}
-				if (ts->ComPort<=ts->MaxComPort) {
-					w = ts->ComPort-1;
-				}
-
 			}
 			SendDlgItemMessage(Dialog, IDC_SERIALPORT, CB_SETCURSEL, w, 0);
 
@@ -1307,21 +1281,15 @@
 
 			// \x8C\xBB\x8D݂̐ڑ\xB1\x8F\xF3\x91ԂƐV\x82\xB5\x82\xA2\x83|\x81[\x83g\x94ԍ\x86\x82̑g\x82ݍ\x87\x82킹\x82ŁA\x90ڑ\xB1\x8F\x88\x97\x9D\x82\xAA\x95ς\xED\x82邽\x82߁A
 			// \x82\xBB\x82\xEA\x82ɉ\x9E\x82\xB6\x82\xC4OK\x83{\x83^\x83\x93\x82̃\x89\x83x\x83\x8B\x96\xBC\x82\xF0\x90؂\xE8\x91ւ\xA6\x82\xE9\x81B
-			serial_dlg_change_OK_button(Dialog, ComPortTable[w], ts->UILanguageFileW);
+			serial_dlg_change_OK_button(Dialog, dlg_data->ComPortInfoPtr[w].port_no, ts->UILanguageFileW);
 
 			return TRUE;
 
-		case WM_DESTROY:
-			// COM\x83|\x81[\x83g\x82̏ڍ׏\xEE\x95\xF1\x82\xF0\x89\xF0\x95\xFA\x82\xB7\x82\xE9\x81B
-			ComPortInfoFree(ComPortInfoPtr, ComPortInfoCount);
-			ComPortInfoPtr = NULL;
-			ComPortInfoCount = 0;
-			break;
-
 		case WM_COMMAND:
 			switch (LOWORD(wParam)) {
 				case IDOK:
 					if ( ts!=NULL ) {
+						char Temp[128];
 						memset(Temp, 0, sizeof(Temp));
 						GetDlgItemText(Dialog, IDC_SERIALPORT, Temp, sizeof(Temp)-1);
 						if (strncmp(Temp, "COM", 3) == 0 && Temp[3] != '\0') {
@@ -1396,11 +1364,12 @@
 				case IDC_SERIALPORT:
 					switch (HIWORD(wParam)) {
 					case CBN_SELCHANGE: // \x83\x8A\x83X\x83g\x82\xA9\x82\xE7COM\x83|\x81[\x83g\x82\xAA\x91I\x91\xF0\x82\xB3\x82ꂽ
+						int portno;
 						sel = SendDlgItemMessage(Dialog, IDC_SERIALPORT, CB_GETCURSEL, 0, 0);
-						portno = ComPortTable[sel];  // \x90V\x82\xB5\x82\xA2\x83|\x81[\x83g\x94ԍ\x86
+						portno = dlg_data->ComPortInfoPtr[sel].port_no;	 // \x83|\x81[\x83g\x94ԍ\x86
 
 						// \x8Fڍ׏\xEE\x95\xF1\x82\xF0\x95\\x8E\xA6\x82\xB7\x82\xE9
-						serial_dlg_set_comport_info(Dialog, ComPortTable[sel], ComPortDesc[sel]);
+						serial_dlg_set_comport_info(Dialog, dlg_data, sel);
 
 						// \x8C\xBB\x8D݂̐ڑ\xB1\x8F\xF3\x91ԂƐV\x82\xB5\x82\xA2\x83|\x81[\x83g\x94ԍ\x86\x82̑g\x82ݍ\x87\x82킹\x82ŁA\x90ڑ\xB1\x8F\x88\x97\x9D\x82\xAA\x95ς\xED\x82邽\x82߁A
 						// \x82\xBB\x82\xEA\x82ɉ\x9E\x82\xB6\x82\xC4OK\x83{\x83^\x83\x93\x82̃\x89\x83x\x83\x8B\x96\xBC\x82\xF0\x90؂\xE8\x91ւ\xA6\x82\xE9\x81B
@@ -3100,10 +3069,18 @@
 
 BOOL WINAPI _SetupSerialPort(HWND WndParent, PTTSet ts)
 {
-	return
-		(BOOL)DialogBoxParam(hInst,
-		                     MAKEINTRESOURCE(IDD_SERIALDLG),
-		                     WndParent, SerialDlg, (LPARAM)ts);
+	BOOL r;
+	SerialDlgData *dlg_data = calloc(sizeof(*dlg_data), 1);
+	dlg_data->pts = ts;
+	dlg_data->ComPortInfoPtr = ComPortInfoGet(&dlg_data->ComPortInfoCount);
+
+	r = (BOOL)DialogBoxParam(hInst,
+							 MAKEINTRESOURCE(IDD_SERIALDLG),
+							 WndParent, SerialDlg, (LPARAM)dlg_data);
+
+	ComPortInfoFree(dlg_data->ComPortInfoPtr, dlg_data->ComPortInfoCount);
+	free(dlg_data);
+	return r;
 }
 
 BOOL WINAPI _SetupTCPIP(HWND WndParent, PTTSet ts)


ttssh2-commit メーリングリストの案内
Back to archive index