diff options
author | Vo Trung Chi <chi.votrung@vn.bosch.com> | 2019-10-16 15:28:19 +0200 |
---|---|---|
committer | Saya Sugiura <39760799+ssugiura@users.noreply.github.com> | 2019-10-24 09:43:23 +0200 |
commit | 1cabce7f04775990533bc8e066497e456350249a (patch) | |
tree | bf8947caf0a1089a353219f5b6243333ae405148 /src | |
parent | b55972cd33df6e02ffcefcb577fbb83fb72b2ef8 (diff) | |
download | DLT-daemon-1cabce7f04775990533bc8e066497e456350249a.tar.gz |
libdlt: fix memory leak
This commit is fixed the memory leak in case the dlt application do
reattach to the daemon.
Signed-off-by: Vo Trung Chi <chi.votrung@vn.bosch.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/dlt_common.c | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c index 4b56fc1..b699ad3 100644 --- a/src/shared/dlt_common.c +++ b/src/shared/dlt_common.c @@ -1855,19 +1855,31 @@ DltReturnValue dlt_vnlog(int prio, size_t size, const char *format, ...) DltReturnValue dlt_receiver_init(DltReceiver *receiver, int fd, int buffersize) { - if (receiver == NULL) + if (NULL == receiver) return DLT_RETURN_WRONG_PARAMETER; - receiver->lastBytesRcvd = 0; - receiver->bytesRcvd = 0; - receiver->totalBytesRcvd = 0; - receiver->buffersize = buffersize; receiver->fd = fd; - receiver->buffer = (char *)malloc(receiver->buffersize); - receiver->backup_buf = NULL; - if (receiver->buffer == NULL) { + /** Reuse the receiver buffer if it exists and the buffer size + * is not changed. If not, free the old one and allocate a new buffer. + */ + if ((NULL != receiver->buffer) && (buffersize != receiver->buffersize)) { + free(receiver->buffer); + receiver->buffer = NULL; + } + + if (NULL == receiver->buffer) { + receiver->lastBytesRcvd = 0; + receiver->bytesRcvd = 0; + receiver->totalBytesRcvd = 0; receiver->buf = NULL; + receiver->backup_buf = NULL; + receiver->buffer = (char *)calloc(1, buffersize); + receiver->buffersize = buffersize; + } + + if (NULL == receiver->buffer) { + dlt_log(LOG_ERR, "allocate memory for receiver buffer failed.\n"); return DLT_RETURN_ERROR; } else { |