qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] Fix some memory leaks in test-visitor-serialization
@ 2013-03-26 21:46 Stefan Berger
  2013-03-27 21:47 ` mdroth
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Berger @ 2013-03-26 21:46 UTC (permalink / raw)
  To: qemu-trivial, Michael Roth; +Cc: qemu-devel

This patch fixes some of the memory leaks in test-visitor-serialization 
but not all of them.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>

---
  tests/test-visitor-serialization.c |   17 ++++++++++++++---
  1 file changed, 14 insertions(+), 3 deletions(-)

Index: qemu-git.pt/tests/test-visitor-serialization.c
===================================================================
--- qemu-git.pt.orig/tests/test-visitor-serialization.c
+++ qemu-git.pt/tests/test-visitor-serialization.c
@@ -258,6 +258,7 @@ static void test_primitives(gconstpointe
      g_assert(pt_copy != NULL);
      if (pt->type == PTYPE_STRING) {
          g_assert_cmpstr(pt->value.string, ==, pt_copy->value.string);
+        g_free((char *)pt_copy->value.string);
      } else if (pt->type == PTYPE_NUMBER) {
          /* we serialize with %f for our reference visitors, so rather 
than fuzzy
           * floating math to test "equality", just compare the 
formatted values
@@ -275,6 +276,7 @@ static void test_primitives(gconstpointe

      ops->cleanup(serialize_data);
      g_free(args);
+    g_free(pt_copy);
  }

  static void test_struct(gconstpointer opaque)
@@ -637,6 +639,7 @@ PrimitiveType pt_values[] = {
  /* visitor-specific op implementations */

  typedef struct QmpSerializeData {
+    QObject *obj;
      QmpOutputVisitor *qov;
      QmpInputVisitor *qiv;
  } QmpSerializeData;
@@ -656,10 +659,10 @@ static void qmp_deserialize(void **nativ
  {
      QmpSerializeData *d = datap;
      QString *output_json = 
qobject_to_json(qmp_output_get_qobject(d->qov));
-    QObject *obj = qobject_from_json(qstring_get_str(output_json));

+    d->obj = qobject_from_json(qstring_get_str(output_json));
      QDECREF(output_json);
-    d->qiv = qmp_input_visitor_new(obj);
+    d->qiv = qmp_input_visitor_new(d->obj);
      visit(qmp_input_get_visitor(d->qiv), native_out, errp);
  }

@@ -668,9 +671,13 @@ static void qmp_cleanup(void *datap)
      QmpSerializeData *d = datap;
      qmp_output_visitor_cleanup(d->qov);
      qmp_input_visitor_cleanup(d->qiv);
+    qobject_decref(d->obj);
+
+    g_free(d);
  }

  typedef struct StringSerializeData {
+    char *string;
      StringOutputVisitor *sov;
      StringInputVisitor *siv;
  } StringSerializeData;
@@ -690,15 +697,19 @@ static void string_deserialize(void **na
  {
      StringSerializeData *d = datap;

-    d->siv = string_input_visitor_new(string_output_get_string(d->sov));
+    d->string = string_output_get_string(d->sov);
+    d->siv = string_input_visitor_new(d->string);
      visit(string_input_get_visitor(d->siv), native_out, errp);
  }

  static void string_cleanup(void *datap)
  {
      StringSerializeData *d = datap;
+
      string_output_visitor_cleanup(d->sov);
      string_input_visitor_cleanup(d->siv);
+    g_free(d->string);
+    g_free(d);
  }

  /* visitor registration, test harness */

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [PATCH] Fix some memory leaks in test-visitor-serialization
  2013-03-26 21:46 [Qemu-devel] [PATCH] Fix some memory leaks in test-visitor-serialization Stefan Berger
@ 2013-03-27 21:47 ` mdroth
  2013-03-28  9:36   ` [Qemu-devel] [Qemu-trivial] " Stefan Hajnoczi
  0 siblings, 1 reply; 3+ messages in thread
From: mdroth @ 2013-03-27 21:47 UTC (permalink / raw)
  To: Stefan Berger; +Cc: qemu-trivial, qemu-devel

On Tue, Mar 26, 2013 at 05:46:55PM -0400, Stefan Berger wrote:
> This patch fixes some of the memory leaks in
> test-visitor-serialization but not all of them.
> 
> Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
> 
> ---
>  tests/test-visitor-serialization.c |   17 ++++++++++++++---
>  1 file changed, 14 insertions(+), 3 deletions(-)
> 
> Index: qemu-git.pt/tests/test-visitor-serialization.c
> ===================================================================
> --- qemu-git.pt.orig/tests/test-visitor-serialization.c
> +++ qemu-git.pt/tests/test-visitor-serialization.c
> @@ -258,6 +258,7 @@ static void test_primitives(gconstpointe
>      g_assert(pt_copy != NULL);
>      if (pt->type == PTYPE_STRING) {
>          g_assert_cmpstr(pt->value.string, ==, pt_copy->value.string);
> +        g_free((char *)pt_copy->value.string);
>      } else if (pt->type == PTYPE_NUMBER) {
>          /* we serialize with %f for our reference visitors, so
> rather than fuzzy
>           * floating math to test "equality", just compare the
> formatted values
> @@ -275,6 +276,7 @@ static void test_primitives(gconstpointe
> 
>      ops->cleanup(serialize_data);
>      g_free(args);
> +    g_free(pt_copy);
>  }
> 
>  static void test_struct(gconstpointer opaque)
> @@ -637,6 +639,7 @@ PrimitiveType pt_values[] = {
>  /* visitor-specific op implementations */
> 
>  typedef struct QmpSerializeData {
> +    QObject *obj;
>      QmpOutputVisitor *qov;
>      QmpInputVisitor *qiv;
>  } QmpSerializeData;
> @@ -656,10 +659,10 @@ static void qmp_deserialize(void **nativ
>  {
>      QmpSerializeData *d = datap;
>      QString *output_json =
> qobject_to_json(qmp_output_get_qobject(d->qov));
> -    QObject *obj = qobject_from_json(qstring_get_str(output_json));
> 
> +    d->obj = qobject_from_json(qstring_get_str(output_json));
>      QDECREF(output_json);
> -    d->qiv = qmp_input_visitor_new(obj);
> +    d->qiv = qmp_input_visitor_new(d->obj);
>      visit(qmp_input_get_visitor(d->qiv), native_out, errp);
>  }
> 
> @@ -668,9 +671,13 @@ static void qmp_cleanup(void *datap)
>      QmpSerializeData *d = datap;
>      qmp_output_visitor_cleanup(d->qov);
>      qmp_input_visitor_cleanup(d->qiv);
> +    qobject_decref(d->obj);

We can actually call this just after passing the qobject to
qmp_input_visitor_new(), which will lead to it getting cleaned up
in qmp_input_visitor_cleanup(). That way we wouldn't need to hold
on to the reference in d->obj.

But this works too, and looks like string visitor still requires
we hang on to the reference to free it, so either way:

Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>

> +
> +    g_free(d);
>  }
> 
>  typedef struct StringSerializeData {
> +    char *string;
>      StringOutputVisitor *sov;
>      StringInputVisitor *siv;
>  } StringSerializeData;
> @@ -690,15 +697,19 @@ static void string_deserialize(void **na
>  {
>      StringSerializeData *d = datap;
> 
> -    d->siv = string_input_visitor_new(string_output_get_string(d->sov));
> +    d->string = string_output_get_string(d->sov);
> +    d->siv = string_input_visitor_new(d->string);
>      visit(string_input_get_visitor(d->siv), native_out, errp);
>  }
> 
>  static void string_cleanup(void *datap)
>  {
>      StringSerializeData *d = datap;
> +
>      string_output_visitor_cleanup(d->sov);
>      string_input_visitor_cleanup(d->siv);
> +    g_free(d->string);
> +    g_free(d);
>  }
> 
>  /* visitor registration, test harness */
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [Qemu-trivial] [PATCH] Fix some memory leaks in test-visitor-serialization
  2013-03-27 21:47 ` mdroth
@ 2013-03-28  9:36   ` Stefan Hajnoczi
  0 siblings, 0 replies; 3+ messages in thread
From: Stefan Hajnoczi @ 2013-03-28  9:36 UTC (permalink / raw)
  To: mdroth; +Cc: qemu-trivial, qemu-devel, Stefan Berger

On Wed, Mar 27, 2013 at 04:47:15PM -0500, mdroth wrote:
> On Tue, Mar 26, 2013 at 05:46:55PM -0400, Stefan Berger wrote:
> > This patch fixes some of the memory leaks in
> > test-visitor-serialization but not all of them.
> > 
> > Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
> > 
> > ---
> >  tests/test-visitor-serialization.c |   17 ++++++++++++++---
> >  1 file changed, 14 insertions(+), 3 deletions(-)
> > 
> > Index: qemu-git.pt/tests/test-visitor-serialization.c
> > ===================================================================
> > --- qemu-git.pt.orig/tests/test-visitor-serialization.c
> > +++ qemu-git.pt/tests/test-visitor-serialization.c
> > @@ -258,6 +258,7 @@ static void test_primitives(gconstpointe
> >      g_assert(pt_copy != NULL);
> >      if (pt->type == PTYPE_STRING) {
> >          g_assert_cmpstr(pt->value.string, ==, pt_copy->value.string);
> > +        g_free((char *)pt_copy->value.string);
> >      } else if (pt->type == PTYPE_NUMBER) {
> >          /* we serialize with %f for our reference visitors, so
> > rather than fuzzy
> >           * floating math to test "equality", just compare the
> > formatted values
> > @@ -275,6 +276,7 @@ static void test_primitives(gconstpointe
> > 
> >      ops->cleanup(serialize_data);
> >      g_free(args);
> > +    g_free(pt_copy);
> >  }
> > 
> >  static void test_struct(gconstpointer opaque)
> > @@ -637,6 +639,7 @@ PrimitiveType pt_values[] = {
> >  /* visitor-specific op implementations */
> > 
> >  typedef struct QmpSerializeData {
> > +    QObject *obj;
> >      QmpOutputVisitor *qov;
> >      QmpInputVisitor *qiv;
> >  } QmpSerializeData;
> > @@ -656,10 +659,10 @@ static void qmp_deserialize(void **nativ
> >  {
> >      QmpSerializeData *d = datap;
> >      QString *output_json =
> > qobject_to_json(qmp_output_get_qobject(d->qov));
> > -    QObject *obj = qobject_from_json(qstring_get_str(output_json));
> > 
> > +    d->obj = qobject_from_json(qstring_get_str(output_json));
> >      QDECREF(output_json);
> > -    d->qiv = qmp_input_visitor_new(obj);
> > +    d->qiv = qmp_input_visitor_new(d->obj);
> >      visit(qmp_input_get_visitor(d->qiv), native_out, errp);
> >  }
> > 
> > @@ -668,9 +671,13 @@ static void qmp_cleanup(void *datap)
> >      QmpSerializeData *d = datap;
> >      qmp_output_visitor_cleanup(d->qov);
> >      qmp_input_visitor_cleanup(d->qiv);
> > +    qobject_decref(d->obj);
> 
> We can actually call this just after passing the qobject to
> qmp_input_visitor_new(), which will lead to it getting cleaned up
> in qmp_input_visitor_cleanup(). That way we wouldn't need to hold
> on to the reference in d->obj.
> 
> But this works too, and looks like string visitor still requires
> we hang on to the reference to free it, so either way:
> 
> Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>

Please decref right away like mdroth suggests so the obj field doesn't
need to be introduced.

Stefan

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-03-28  9:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-26 21:46 [Qemu-devel] [PATCH] Fix some memory leaks in test-visitor-serialization Stefan Berger
2013-03-27 21:47 ` mdroth
2013-03-28  9:36   ` [Qemu-devel] [Qemu-trivial] " Stefan Hajnoczi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).