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

Fix memory leaks #367

Merged
merged 2 commits into from
Mar 18, 2022
Merged

Fix memory leaks #367

merged 2 commits into from
Mar 18, 2022

Conversation

rbuj
Copy link
Contributor

@rbuj rbuj commented Oct 22, 2021

No description provided.

@rbuj rbuj changed the title gpm-statistics: fix memory leak Fix memory leaks Oct 22, 2021
@mbkma
Copy link
Member

mbkma commented Dec 5, 2021

In the first commit there is not only a memory leaked fixed. Can you update the commit message describing what it does please?

@rbuj
Copy link
Contributor Author

rbuj commented Dec 6, 2021

The local and global variable called history_type is a static string.

static const gchar *history_type;

@rbuj rbuj force-pushed the gpm-statistics-memory-leak branch from d09a320 to a32c45a Compare December 8, 2021 10:08
@rbuj
Copy link
Contributor Author

rbuj commented Dec 8, 2021

@mbkma done

@rbuj rbuj force-pushed the gpm-statistics-memory-leak branch from a32c45a to fb4348c Compare December 8, 2021 10:20
rbuj added 2 commits December 8, 2021 12:39
In addition, it adds enumerations to access indexed arrays.
In addition, it adds enumerations to access indexed arrays.
@rbuj rbuj force-pushed the gpm-statistics-memory-leak branch from fb4348c to 3e0af8c Compare December 8, 2021 11:40
@mbkma
Copy link
Member

mbkma commented Dec 8, 2021

Thanks, yeah I noticed that you were right. I have not time to test this right now, I hope to have more time soon.

@raveit65
Copy link
Member

What a huge PR, how can this be reviewed?

@rbuj
Copy link
Contributor Author

rbuj commented Mar 12, 2022

Just compile the code with Address Sanitizer enabled. Next, run mate-power-statistics and mate-power-preferences from the command line.

  • mate-power-preferences: It fixes the memory leak in mate-power-preferences, because missing g_option_context_free.
  • mate-power-statistics: gpm_upower_get_device_icon function returns a new allocated string.
    return filename;
  • mate-power-statistics: Instead of storing a string using a global variable (history_type & stats_type), use an enum, as working with enums is more efficient, avoiding the memory leak.

g_settings_get_string returns a new allocated memory, not a const gchar *:

before

static const gchar *history_type;
static const gchar *stats_type;
history_type = g_settings_get_string (settings, GPM_SETTINGS_INFO_HISTORY_TYPE);
stats_type = g_settings_get_string (settings, GPM_SETTINGS_INFO_STATS_TYPE);

@raveit65
Copy link
Member

It seems that those leaks are fixed in my test.

[rave@mother ~]$ /usr/local/bin/mate-power-statistics 

=================================================================
==84469==ERROR: LeakSanitizer: detected memory leaks


Direct leak of 26 byte(s) in 2 object(s) allocated from:
    #0 0x7fe86231591f in __interceptor_malloc (/lib64/libasan.so.6+0xae91f)
    #1 0x7fe86114e938 in g_malloc (/lib64/libglib-2.0.so.0+0x5d938)

Direct leak of 14 byte(s) in 1 object(s) allocated from:
    #0 0x7fe86231591f in __interceptor_malloc (/lib64/libasan.so.6+0xae91f)
    #1 0x7fe860e209cf in __vasprintf_internal (/lib64/libc.so.6+0x7e9cf)


[rave@mother ~]$ /usr/local/bin/mate-power-preferences 

=================================================================
==85806==ERROR: LeakSanitizer: detected memory leaks


Direct leak of 88 byte(s) in 1 object(s) allocated from:
    #0 0x7faee4df6ad7 in calloc (/lib64/libasan.so.6+0xaead7)
    #1 0x7faee3c2fe60 in g_malloc0 (/lib64/libglib-2.0.so.0+0x5de60)

Indirect leak of 112 byte(s) in 1 object(s) allocated from:
    #0 0x7faee4df6ad7 in calloc (/lib64/libasan.so.6+0xaead7)
    #1 0x7faee3c2fe60 in g_malloc0 (/lib64/libglib-2.0.so.0+0x5de60)

Indirect leak of 61 byte(s) in 3 object(s) allocated from:
    #0 0x7faee4df691f in __interceptor_malloc (/lib64/libasan.so.6+0xae91f)
    #1 0x7faee3c2f938 in g_malloc (/lib64/libglib-2.0.so.0+0x5d938)

Copy link
Member

@raveit65 raveit65 left a comment

Choose a reason for hiding this comment

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

LGTM,
runs fine, but needs to be reviewed by another reviewer because it is a huge code change.

@raveit65 raveit65 requested a review from a team March 13, 2022 12:26
Copy link
Member

@lukefromdc lukefromdc left a comment

Choose a reason for hiding this comment

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

While mate-power-statistics doesn't return any data on my desktop, the empty graph does come up and nothing crashes. The preference dialog behaves the same as in master.I can turn the display of the m-p-m status icon on and off same as before. Note that problems when fixing memory leaks often lead to segfaults and I didn't get any crashes.

@lukefromdc
Copy link
Member

Testing this on the converted Chromebook, mate-power-statistics works fine, as do options in the "on battery power" tab in the preferences dialog. Will merge

@lukefromdc lukefromdc merged commit 67d510c into master Mar 18, 2022
@lukefromdc lukefromdc deleted the gpm-statistics-memory-leak branch March 18, 2022 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants