diff mbox

[PATCHv2] vl.c: Fix memory leak in qemu_register_machine

Message ID 1395228267-34202-1-git-send-email-borntraeger@de.ibm.com
State New
Headers show

Commit Message

Christian Borntraeger March 19, 2014, 11:24 a.m. UTC
since commit 261747f176f6 (vl: Use MachineClass instead of global
QEMUMachine list) valgrind complains about the following:

==54082== 57 bytes in 3 blocks are definitely lost in loss record 365 of
729
==54082==    at 0x4031AFE: malloc (vg_replace_malloc.c:292)
==54082==    by 0x4145569: g_malloc (in
/usr/lib64/libglib-2.0.so.0.3400.2)
==54082==    by 0x415F9E9: g_strconcat (in
/usr/lib64/libglib-2.0.so.0.3400.2)
==54082==    by 0x80157FE7: qemu_register_machine (vl.c:1597)
==54082==    by 0x80208E6B: module_call_init (module.c:105)
==54082==    by 0x80013B91: main (vl.c:3000)

Turns out that valgrind is right. We simply forget the memory that
g_strconcat has allocated. Lets free it after the type_register.
We need a 2nd variable due to constness of the name part of the
type structure.

Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
---
 vl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Andreas Färber March 19, 2014, 12:10 p.m. UTC | #1
Am 19.03.2014 12:24, schrieb Christian Borntraeger:
> since commit 261747f176f6 (vl: Use MachineClass instead of global
> QEMUMachine list) valgrind complains about the following:
> 
> ==54082== 57 bytes in 3 blocks are definitely lost in loss record 365 of
> 729
> ==54082==    at 0x4031AFE: malloc (vg_replace_malloc.c:292)
> ==54082==    by 0x4145569: g_malloc (in
> /usr/lib64/libglib-2.0.so.0.3400.2)
> ==54082==    by 0x415F9E9: g_strconcat (in
> /usr/lib64/libglib-2.0.so.0.3400.2)
> ==54082==    by 0x80157FE7: qemu_register_machine (vl.c:1597)
> ==54082==    by 0x80208E6B: module_call_init (module.c:105)
> ==54082==    by 0x80013B91: main (vl.c:3000)
> 
> Turns out that valgrind is right. We simply forget the memory that
> g_strconcat has allocated. Lets free it after the type_register.
> We need a 2nd variable due to constness of the name part of the
> type structure.
> 
> Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>

Thanks, applied to qom-next (using standard syntax for function names):
https://github.com/afaerber/qemu-cpu/commits/qom-next

Andreas
Michael S. Tsirkin March 19, 2014, 1:21 p.m. UTC | #2
On Wed, Mar 19, 2014 at 12:24:27PM +0100, Christian Borntraeger wrote:
> since commit 261747f176f6 (vl: Use MachineClass instead of global
> QEMUMachine list) valgrind complains about the following:
> 
> ==54082== 57 bytes in 3 blocks are definitely lost in loss record 365 of
> 729
> ==54082==    at 0x4031AFE: malloc (vg_replace_malloc.c:292)
> ==54082==    by 0x4145569: g_malloc (in
> /usr/lib64/libglib-2.0.so.0.3400.2)
> ==54082==    by 0x415F9E9: g_strconcat (in
> /usr/lib64/libglib-2.0.so.0.3400.2)
> ==54082==    by 0x80157FE7: qemu_register_machine (vl.c:1597)
> ==54082==    by 0x80208E6B: module_call_init (module.c:105)
> ==54082==    by 0x80013B91: main (vl.c:3000)
> 
> Turns out that valgrind is right. We simply forget the memory that
> g_strconcat has allocated. Lets free it after the type_register.
> We need a 2nd variable due to constness of the name part of the
> type structure.
> 
> Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>

Reviewed-by: Michael S. Tsirkin <mst@redhat.com>

Andreas can you pick this up pls?

> ---
>  vl.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/vl.c b/vl.c
> index f0fe48b..0e82f06 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -1587,14 +1587,16 @@ static void machine_class_init(ObjectClass *oc, void *data)
>  
>  int qemu_register_machine(QEMUMachine *m)
>  {
> +    char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
>      TypeInfo ti = {
> -        .name       = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL),
> +        .name       = name,
>          .parent     = TYPE_MACHINE,
>          .class_init = machine_class_init,
>          .class_data = (void *)m,
>      };
>  
>      type_register(&ti);
> +    g_free(name);
>  
>      return 0;
>  }
> -- 
> 1.8.4.2
Andreas Färber March 19, 2014, 8:01 p.m. UTC | #3
Am 19.03.2014 14:21, schrieb Michael S. Tsirkin:
> On Wed, Mar 19, 2014 at 12:24:27PM +0100, Christian Borntraeger wrote:
>> since commit 261747f176f6 (vl: Use MachineClass instead of global
>> QEMUMachine list) valgrind complains about the following:
>>
>> ==54082== 57 bytes in 3 blocks are definitely lost in loss record 365 of
>> 729
>> ==54082==    at 0x4031AFE: malloc (vg_replace_malloc.c:292)
>> ==54082==    by 0x4145569: g_malloc (in
>> /usr/lib64/libglib-2.0.so.0.3400.2)
>> ==54082==    by 0x415F9E9: g_strconcat (in
>> /usr/lib64/libglib-2.0.so.0.3400.2)
>> ==54082==    by 0x80157FE7: qemu_register_machine (vl.c:1597)
>> ==54082==    by 0x80208E6B: module_call_init (module.c:105)
>> ==54082==    by 0x80013B91: main (vl.c:3000)
>>
>> Turns out that valgrind is right. We simply forget the memory that
>> g_strconcat has allocated. Lets free it after the type_register.
>> We need a 2nd variable due to constness of the name part of the
>> type structure.
>>
>> Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
> 
> Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
> 
> Andreas can you pick this up pls?

I already had - updated with your Rb now.

Thanks,
Andreas
diff mbox

Patch

diff --git a/vl.c b/vl.c
index f0fe48b..0e82f06 100644
--- a/vl.c
+++ b/vl.c
@@ -1587,14 +1587,16 @@  static void machine_class_init(ObjectClass *oc, void *data)
 
 int qemu_register_machine(QEMUMachine *m)
 {
+    char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
     TypeInfo ti = {
-        .name       = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL),
+        .name       = name,
         .parent     = TYPE_MACHINE,
         .class_init = machine_class_init,
         .class_data = (void *)m,
     };
 
     type_register(&ti);
+    g_free(name);
 
     return 0;
 }