Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve compress lead mem leak #666

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/offlinelogstorage/dlt_offline_logstorage.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ DLT_STATIC void dlt_logstorage_filter_config_free(DltLogStorageFilterConfig *dat
data->ecuid = NULL;
}

if (data->log != NULL)
fclose(data->log);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain a little bit why we move the code here to other lines?
Is there any reasons for closing the gzip first and then close the log?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the order of opening is fopen gzipopen, so I think the order of closing is best gzclose, fclose


#ifdef DLT_LOGSTORAGE_USE_GZIP
if (data->gzlog != NULL)
gzclose(data->gzlog);
#endif

if (data->log != NULL)
fclose(data->log);

if (data->cache != NULL) {
free(data->cache);
data->cache = NULL;
Expand Down
12 changes: 10 additions & 2 deletions src/offlinelogstorage/dlt_offline_logstorage_behavior.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,15 @@ int dlt_logstorage_storage_dir_info(DltLogStorageUserConfig *file_config,
}

char* suffix = NULL;
for (i = 0; i < cnt; i++) {
if (config->gzip_compression) {
if (config->gzip_compression) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think allocating the suffix here multiple times in the loops was the only cause for the memory leak. Or is there another memleak fixed in this PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I will test this case separately when I have time to see if it matches the guess

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think allocating the suffix here multiple times in the loops was the only cause for the memory leak. Or is there another memleak fixed in this PR?

After testing, it was found that there are two main reasons for memory leaks: 1. fclose needs to be added 2.Allocate memory in a loop

suffix = strdup(".dlt.gz");
}
else {
suffix = strdup(".dlt");
}

for (i = 0; i < cnt; i++) {

int len = 0;
len = strlen(file_name);
Expand Down Expand Up @@ -501,6 +503,7 @@ DLT_STATIC void dlt_logstorage_open_log_output_file(DltLogStorageFilterConfig *c
#ifdef DLT_LOGSTORAGE_USE_GZIP
dlt_vlog(LOG_DEBUG, "%s: Opening GZIP log file\n", __func__);
config->gzlog = gzdopen(config->fd, mode);
config->log = file;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be no need to store config->log here. As per my understanding gzclose() will close the passed fd from gzdopen()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to my tests, after gzclose, fclose is also needed. This part of the test is included in #655

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michael-methner
I did a test to remove fclose, and the results are as follows: fclose is necessary, and the currently submitted code is necessary.

add fclose
add fclose

delete fclose
delete_flocse

#endif
} else {
dlt_vlog(LOG_DEBUG, "%s: Opening log file\n", __func__);
Expand Down Expand Up @@ -912,6 +915,11 @@ DLT_STATIC void dlt_logstorage_close_file(DltLogStorageFilterConfig *config)
gzclose(config->gzlog);
config->gzlog = NULL;
}

if (config->log) {
hush-soul marked this conversation as resolved.
Show resolved Hide resolved
fclose(config->log);
config->log = NULL;
}
#endif
if (config->log) {
fclose(config->log);
Expand Down