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;
|
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() {
|
||||||
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() {
|
||||||
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() {
|
||||||
|
|
||||||
taos_close(shell.conn);
|
taos_close(shell.conn);
|
||||||
shellWriteHistory();
|
shellWriteHistory();
|
||||||
|
shellCleanupHistory();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -996,5 +1009,6 @@ int32_t shellExecute() {
|
||||||
taosThreadClear(&shell.pid);
|
taosThreadClear(&shell.pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shellCleanupHistory();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue