<div dir="ltr">Gotcha. I guess the GError* and gchar* leaks are probably ok if the program aborts.</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jun 3, 2014 at 1:56 AM, Aleksander Morgado <span dir="ltr"><<a href="mailto:aleksander@aleksander.es" target="_blank">aleksander@aleksander.es</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">On 03/06/14 04:10, Ben Chan wrote:<br>
> This patch fixes a few memory leaks and clears a few GError pointers<br>
> before reusing them.<br>
> ---<br>
<br>
<br>
</div>g_error() explicitly aborts the program; so there really is no point in<br>
clearing the error afterwards because the program will no longer be running:<br>
<br>
<a href="https://developer.gnome.org/glib/stable/glib-Message-Logging.html#g-error" target="_blank">https://developer.gnome.org/glib/stable/glib-Message-Logging.html#g-error</a><br>
<div><div class="h5"><br>
<br>
>  plugins/tests/test-fixture.c      | 20 +++++++++++++++-----<br>
>  plugins/tests/test-port-context.c | 29 +++++++++++++++++++++--------<br>
>  2 files changed, 36 insertions(+), 13 deletions(-)<br>
><br>
> diff --git a/plugins/tests/test-fixture.c b/plugins/tests/test-fixture.c<br>
> index 6b10f47..2fe1d59 100644<br>
> --- a/plugins/tests/test-fixture.c<br>
> +++ b/plugins/tests/test-fixture.c<br>
> @@ -34,8 +34,10 @@ test_fixture_setup (TestFixture *fixture)<br>
><br>
>      /* Create DBus connection */<br>
>      fixture->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);<br>
> -    if (fixture->connection == NULL)<br>
> +    if (fixture->connection == NULL) {<br>
>          g_error ("Error getting connection to test bus: %s", error->message);<br>
> +        g_clear_error (&error);<br>
> +    }<br>
><br>
>      /* Ping to autostart MM; wait up to 3s */<br>
>      result = g_dbus_connection_call_sync (fixture->connection,<br>
> @@ -49,8 +51,10 @@ test_fixture_setup (TestFixture *fixture)<br>
>                                            3000, /* timeout, ms */<br>
>                                            NULL, /* cancellable */<br>
>                                            &error);<br>
> -    if (!result)<br>
> +    if (!result) {<br>
>          g_error ("Error starting ModemManager in test bus: %s", error->message);<br>
> +        g_clear_error (&error);<br>
> +    }<br>
>      g_variant_unref (result);<br>
><br>
>      /* Create the proxy that we're going to test */<br>
> @@ -60,8 +64,10 @@ test_fixture_setup (TestFixture *fixture)<br>
>                                                    "/org/freedesktop/ModemManager1",<br>
>                                                    NULL, /* cancellable */<br>
>                                                    &error);<br>
> -    if (fixture->test == NULL)<br>
> +    if (fixture->test == NULL) {<br>
>          g_error ("Error getting ModemManager test proxy: %s", error->message);<br>
> +        g_clear_error (&error);<br>
> +    }<br>
>  }<br>
><br>
>  void<br>
> @@ -111,8 +117,10 @@ test_fixture_get_modem (TestFixture *fixture)<br>
>                                     G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,<br>
>                                     NULL, /* cancellable */<br>
>                                     &error);<br>
> -    if (!manager)<br>
> +    if (!manager) {<br>
>          g_error ("Couldn't create manager: %s", error->message);<br>
> +        g_clear_error (&error);<br>
> +    }<br>
><br>
>      /* Find new modem object */<br>
>      while (!found) {<br>
> @@ -157,8 +165,10 @@ test_fixture_no_modem (TestFixture *fixture)<br>
>                                     G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,<br>
>                                     NULL, /* cancellable */<br>
>                                     &error);<br>
> -    if (!manager)<br>
> +    if (!manager) {<br>
>          g_error ("Couldn't create manager: %s", error->message);<br>
> +        g_clear_error (&error);<br>
> +    }<br>
><br>
>      /* Find new modem object */<br>
>      while (!no_modems) {<br>
> diff --git a/plugins/tests/test-port-context.c b/plugins/tests/test-port-context.c<br>
> index cbf202f..53c6ef3 100644<br>
> --- a/plugins/tests/test-port-context.c<br>
> +++ b/plugins/tests/test-port-context.c<br>
> @@ -53,10 +53,16 @@ test_port_context_load_commands (TestPortContext *self,<br>
>      gchar *contents;<br>
>      gchar *current;<br>
><br>
> -    if (!g_file_get_contents (file, &contents, NULL, &error))<br>
> +    if (!g_file_get_contents (file, &contents, NULL, &error)) {<br>
> +        gchar *filename;<br>
> +<br>
> +        filename = g_filename_display_name (file);<br>
>          g_error ("Couldn't load commands file '%s': %s",<br>
> -                 g_filename_display_name (file),<br>
> +                 filename,<br>
>                   error->message);<br>
> +        g_free (filename);<br>
> +        g_clear_error (&error);<br>
> +    }<br>
><br>
>      current = contents;<br>
>      while (current) {<br>
> @@ -198,8 +204,7 @@ connection_readable_cb (GSocket *socket,<br>
><br>
>      if (r < 0) {<br>
>          g_warning ("Error reading from istream: %s", error ? error->message : "unknown");<br>
> -        if (error)<br>
> -            g_error_free (error);<br>
> +        g_clear_error (&error);<br>
>          /* Close the device */<br>
>          connection_close (client);<br>
>          return FALSE;<br>
> @@ -269,20 +274,26 @@ create_socket_service (TestPortContext *self)<br>
>                             G_SOCKET_TYPE_STREAM,<br>
>                             G_SOCKET_PROTOCOL_DEFAULT,<br>
>                             &error);<br>
> -    if (!socket)<br>
> +    if (!socket) {<br>
>          g_error ("Cannot create socket: %s", error->message);<br>
> +        g_clear_error (&error);<br>
> +    }<br>
><br>
>      /* Bind to address */<br>
>      address = (g_unix_socket_address_new_with_type (<br>
>                     self->name,<br>
>                     -1,<br>
>                     G_UNIX_SOCKET_ADDRESS_ABSTRACT));<br>
> -    if (!g_socket_bind (socket, address, TRUE, &error))<br>
> +    if (!g_socket_bind (socket, address, TRUE, &error)) {<br>
>          g_error ("Cannot bind socket: %s", error->message);<br>
> +        g_clear_error (&error);<br>
> +    }<br>
><br>
>      /* Listen */<br>
> -    if (!g_socket_listen (socket, &error))<br>
> +    if (!g_socket_listen (socket, &error)) {<br>
>          g_error ("Cannot listen in socket: %s", error->message);<br>
> +        g_clear_error (&error);<br>
> +    }<br>
><br>
>      /* Create socket service */<br>
>      service = g_socket_service_new ();<br>
> @@ -290,8 +301,10 @@ create_socket_service (TestPortContext *self)<br>
>      if (!g_socket_listener_add_socket (G_SOCKET_LISTENER (service),<br>
>                                         socket,<br>
>                                         NULL, /* don't pass an object, will take a reference */<br>
> -                                       &error))<br>
> +                                       &error)) {<br>
>          g_error ("Cannot add listener to socket: %s", error->message);<br>
> +        g_clear_error (&error);<br>
> +    }<br>
><br>
>      /* Start it */<br>
>      g_socket_service_start (service);<br>
><br>
<br>
<br>
--<br>
</div></div>Aleksander<br>
<a href="https://aleksander.es" target="_blank">https://aleksander.es</a><br>
</blockquote></div><br></div>