From 133a963c7c263511db02b3e3fd2d98a85fbd3a49 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 21 May 2022 21:29:32 +0800 Subject: [PATCH] fix: memory leak while record history in shell --- tools/shell/src/shellEngine.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/shell/src/shellEngine.c b/tools/shell/src/shellEngine.c index d6c295a222..a866488d3a 100644 --- a/tools/shell/src/shellEngine.c +++ b/tools/shell/src/shellEngine.c @@ -742,6 +742,7 @@ void shellReadHistory() { int32_t read_size = 0; while ((read_size = taosGetLineFile(pFile, &line)) != -1) { line[read_size - 1] = '\0'; + taosMemoryFree(pHistory->hist[pHistory->hend]); pHistory->hist[pHistory->hend] = strdup(line); pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE; @@ -763,7 +764,8 @@ void shellWriteHistory() { for (int32_t i = pHistory->hstart; i != pHistory->hend;) { if (pHistory->hist[i] != NULL) { taosFprintfFile(pFile, "%s\n", pHistory->hist[i]); - taosMemoryFreeClear(pHistory->hist[i]); + taosMemoryFree(pHistory->hist[i]); + pHistory->hist[i] = NULL; } i = (i + 1) % SHELL_MAX_HISTORY_SIZE; } @@ -771,6 +773,16 @@ void shellWriteHistory() { taosCloseFile(&pFile); } +void shellCleanupHistory() { + SShellHistory *pHistory = &shell.history; + for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) { + if (pHistory->hist[i] != NULL) { + taosMemoryFree(pHistory->hist[i]); + pHistory->hist[i] = NULL; + } + } +} + void shellPrintError(TAOS_RES *tres, int64_t st) { int64_t et = taosGetTimestampUs(); fprintf(stderr, "\nDB error: %s (%.6fs)\n", taos_errstr(tres), (et - st) / 1E6); @@ -971,6 +983,7 @@ int32_t shellExecute() { taos_close(shell.conn); shellWriteHistory(); + shellCleanupHistory(); return 0; } @@ -996,5 +1009,6 @@ int32_t shellExecute() { taosThreadClear(&shell.pid); } + shellCleanupHistory(); return 0; }