fix: memory leak while record history in shell
This commit is contained in:
parent
740171205e
commit
133a963c7c
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue