未验证 提交 54d6ed46 编写于 作者: S Shengliang Guan 提交者: GitHub

Merge pull request #12817 from taosdata/fix/valgrind

fix: memory leak while record history in shell
...@@ -742,6 +742,7 @@ void shellReadHistory() { ...@@ -742,6 +742,7 @@ void shellReadHistory() {
int32_t read_size = 0; int32_t read_size = 0;
while ((read_size = taosGetLineFile(pFile, &line)) != -1) { while ((read_size = taosGetLineFile(pFile, &line)) != -1) {
line[read_size - 1] = '\0'; line[read_size - 1] = '\0';
taosMemoryFree(pHistory->hist[pHistory->hend]);
pHistory->hist[pHistory->hend] = strdup(line); pHistory->hist[pHistory->hend] = strdup(line);
pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE; pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
...@@ -763,7 +764,8 @@ void shellWriteHistory() { ...@@ -763,7 +764,8 @@ void shellWriteHistory() {
for (int32_t i = pHistory->hstart; i != pHistory->hend;) { for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
if (pHistory->hist[i] != NULL) { if (pHistory->hist[i] != NULL) {
taosFprintfFile(pFile, "%s\n", pHistory->hist[i]); 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; i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
} }
...@@ -771,6 +773,16 @@ void shellWriteHistory() { ...@@ -771,6 +773,16 @@ void shellWriteHistory() {
taosCloseFile(&pFile); 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) { void shellPrintError(TAOS_RES *tres, int64_t st) {
int64_t et = taosGetTimestampUs(); int64_t et = taosGetTimestampUs();
fprintf(stderr, "\nDB error: %s (%.6fs)\n", taos_errstr(tres), (et - st) / 1E6); fprintf(stderr, "\nDB error: %s (%.6fs)\n", taos_errstr(tres), (et - st) / 1E6);
...@@ -971,6 +983,7 @@ int32_t shellExecute() { ...@@ -971,6 +983,7 @@ int32_t shellExecute() {
taos_close(shell.conn); taos_close(shell.conn);
shellWriteHistory(); shellWriteHistory();
shellCleanupHistory();
return 0; return 0;
} }
...@@ -996,5 +1009,6 @@ int32_t shellExecute() { ...@@ -996,5 +1009,6 @@ int32_t shellExecute() {
taosThreadClear(&shell.pid); taosThreadClear(&shell.pid);
} }
shellCleanupHistory();
return 0; return 0;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册