Add destructor function to free pixman_implementation_t structs on exit

Submitted by Siarhei Siamashka on Sept. 18, 2017, 10:46 a.m.

Details

Message ID 1505731566-10853-1-git-send-email-siarhei.siamashka@gmail.com
State New
Headers show
Series "Add destructor function to free pixman_implementation_t structs on exit" ( rev: 1 ) in Pixman

Not browsing as part of any series.

Commit Message

Siarhei Siamashka Sept. 18, 2017, 10:46 a.m.
This fixes a few small memory leaks detected by valgrind. This memory
was allocated once on pixman library load and never freed (but still
was reachable). The fix only helps if the compiler has support for
__attribute__((constructor)) and __attribute__((destructor))
function attributes.

Reported-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Siarhei Siamashka <siarhei.siamashka@gmail.com>
---
 configure.ac    |  1 +
 pixman/pixman.c | 12 ++++++++++++
 2 files changed, 13 insertions(+)

Patch hide | download patch | download mbox

diff --git a/configure.ac b/configure.ac
index e833e45..a592cba 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1047,6 +1047,7 @@  AC_LINK_IFELSE([AC_LANG_SOURCE([[
  */
 static int x = 1;
 static void __attribute__((constructor)) constructor_function () { x = 0; }
+static void __attribute__((destructor)) destructor_function () { x = 0; }
 int main (void) { return x; }
 #else
 #error not gcc or gcc version is older than 2.7
diff --git a/pixman/pixman.c b/pixman/pixman.c
index f932eac..0bcc832 100644
--- a/pixman/pixman.c
+++ b/pixman/pixman.c
@@ -38,6 +38,18 @@  pixman_constructor (void)
 {
     global_implementation = _pixman_choose_implementation ();
 }
+
+static void __attribute__((destructor))
+pixman_destructor (void)
+{
+    pixman_implementation_t *imp = global_implementation;
+    while (imp)
+    {
+	pixman_implementation_t *fallback = imp->fallback;
+	free (imp);
+	imp = fallback;
+    }
+}
 #endif
 
 typedef struct operator_info_t operator_info_t;

Comments

On Mon, 18 Sep 2017 13:46:06 +0300
Siarhei Siamashka <siarhei.siamashka@gmail.com> wrote:

> This fixes a few small memory leaks detected by valgrind. This memory
> was allocated once on pixman library load and never freed (but still
> was reachable). The fix only helps if the compiler has support for
> __attribute__((constructor)) and __attribute__((destructor))
> function attributes.
> 
> Reported-by: Emil Velikov <emil.l.velikov@gmail.com>
> Signed-off-by: Siarhei Siamashka <siarhei.siamashka@gmail.com>
> ---
>  configure.ac    |  1 +
>  pixman/pixman.c | 12 ++++++++++++
>  2 files changed, 13 insertions(+)
> 
> diff --git a/configure.ac b/configure.ac
> index e833e45..a592cba 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -1047,6 +1047,7 @@ AC_LINK_IFELSE([AC_LANG_SOURCE([[
>   */
>  static int x = 1;
>  static void __attribute__((constructor)) constructor_function () { x = 0; }
> +static void __attribute__((destructor)) destructor_function () { x = 0; }
>  int main (void) { return x; }
>  #else
>  #error not gcc or gcc version is older than 2.7
> diff --git a/pixman/pixman.c b/pixman/pixman.c
> index f932eac..0bcc832 100644
> --- a/pixman/pixman.c
> +++ b/pixman/pixman.c
> @@ -38,6 +38,18 @@ pixman_constructor (void)
>  {
>      global_implementation = _pixman_choose_implementation ();
>  }
> +
> +static void __attribute__((destructor))
> +pixman_destructor (void)
> +{
> +    pixman_implementation_t *imp = global_implementation;
> +    while (imp)
> +    {
> +	pixman_implementation_t *fallback = imp->fallback;
> +	free (imp);
> +	imp = fallback;
> +    }
> +}
>  #endif
>  
>  typedef struct operator_info_t operator_info_t;

Hi,

looks good to me.
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>

Didn't test.


Thanks,
pq
On 18 September 2017 at 11:46, Siarhei Siamashka
<siarhei.siamashka@gmail.com> wrote:
> This fixes a few small memory leaks detected by valgrind. This memory
> was allocated once on pixman library load and never freed (but still
> was reachable). The fix only helps if the compiler has support for
> __attribute__((constructor)) and __attribute__((destructor))
> function attributes.
>
> Reported-by: Emil Velikov <emil.l.velikov@gmail.com>
> Signed-off-by: Siarhei Siamashka <siarhei.siamashka@gmail.com>

Looks and works like a charm. FWIW

Reviewed-by: Emil Velikov <emil.velikov@collabora.com>
Tested-by: Emil Velikov <emil.velikov@collabora.com>

-Emil