Patchwork qemu-nbd.c : fix memory leak

login
register
mail settings
Submitter Zhi Hui Li
Date Dec. 2, 2011, 3:34 a.m.
Message ID <1322796863-3475-1-git-send-email-zhihuili@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/128796/
State New
Headers show

Comments

Zhi Hui Li - Dec. 2, 2011, 3:34 a.m.
Signed-off-by: Li Zhi Hui <zhihuili@linux.vnet.ibm.com>
---
 qemu-nbd.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
Mark Wu - Dec. 2, 2011, 7:28 a.m.
On 12/02/2011 11:34 AM, Li Zhi Hui wrote:
> Signed-off-by: Li Zhi Hui<zhihuili@linux.vnet.ibm.com>
> ---
>   qemu-nbd.c |    4 +++-
>   1 files changed, 3 insertions(+), 1 deletions(-)
>
> diff --git a/qemu-nbd.c b/qemu-nbd.c
> index 291cba2..ab7fa6c 100644
> --- a/qemu-nbd.c
> +++ b/qemu-nbd.c
> @@ -500,8 +500,10 @@ int main(int argc, char **argv)
>           sharing_fds[0] = tcp_socket_incoming(bindto, port);
>       }
>
> -    if (sharing_fds[0] == -1)
> +    if (sharing_fds[0] == -1) {
> +        g_free(sharing_fds);
>           return 1;
> +    }
>
>       if (device) {
>           int ret;
Zhihui,

Kernel should free all memory used by the process after it exits.  So  
there's no memory leak even without explicit free.
Stefan Weil - Dec. 2, 2011, 7:42 a.m.
Am 02.12.2011 08:28, schrieb Mark Wu:
> On 12/02/2011 11:34 AM, Li Zhi Hui wrote:
>> Signed-off-by: Li Zhi Hui<zhihuili@linux.vnet.ibm.com>
>> ---
>>   qemu-nbd.c |    4 +++-
>>   1 files changed, 3 insertions(+), 1 deletions(-)
>>
>> diff --git a/qemu-nbd.c b/qemu-nbd.c
>> index 291cba2..ab7fa6c 100644
>> --- a/qemu-nbd.c
>> +++ b/qemu-nbd.c
>> @@ -500,8 +500,10 @@ int main(int argc, char **argv)
>>           sharing_fds[0] = tcp_socket_incoming(bindto, port);
>>       }
>>
>> -    if (sharing_fds[0] == -1)
>> +    if (sharing_fds[0] == -1) {
>> +        g_free(sharing_fds);
>>           return 1;
>> +    }
>>
>>       if (device) {
>>           int ret;
> Zhihui,
>
> Kernel should free all memory used by the process after it exits.  So  
> there's no memory leak even without explicit free.

That's correct. Nevertheless fixing this helps to find other more important
memory leaks with static or dynamic code analyzers like cppcheck or 
valgrind.

Reviewed-by: Stefan Weil <sw@weilnetz.de>
Paolo Bonzini - Dec. 2, 2011, 7:48 a.m.
On 12/02/2011 08:42 AM, Stefan Weil wrote:
>>>
>>> -    if (sharing_fds[0] == -1)
>>> +    if (sharing_fds[0] == -1) {
>>> +        g_free(sharing_fds);
>>>           return 1;
>>> +    }
>>>
>>>       if (device) {
>>>           int ret;
>> Zhihui,
>>
>> Kernel should free all memory used by the process after it exits.  So
>> there's no memory leak even without explicit free.
>
> That's correct. Nevertheless fixing this helps to find other more important
> memory leaks with static or dynamic code analyzers like cppcheck or
> valgrind.
>
> Reviewed-by: Stefan Weil <sw@weilnetz.de>

Please hold off, I'm about to introduce large changes to qemu-nbd for 
asynchronous I/O (including getting rid of sharing_fds in favor of the 
main loop) and and I'd rather avoid gratuitous conflicts.

Paolo
Zhi Hui Li - Dec. 2, 2011, 7:53 a.m.
Thank you very much!
Stefan Hajnoczi - Dec. 2, 2011, 11:08 a.m.
On Fri, Dec 02, 2011 at 08:48:50AM +0100, Paolo Bonzini wrote:
> On 12/02/2011 08:42 AM, Stefan Weil wrote:
> >>>
> >>>-    if (sharing_fds[0] == -1)
> >>>+    if (sharing_fds[0] == -1) {
> >>>+        g_free(sharing_fds);
> >>>          return 1;
> >>>+    }
> >>>
> >>>      if (device) {
> >>>          int ret;
> >>Zhihui,
> >>
> >>Kernel should free all memory used by the process after it exits.  So
> >>there's no memory leak even without explicit free.
> >
> >That's correct. Nevertheless fixing this helps to find other more important
> >memory leaks with static or dynamic code analyzers like cppcheck or
> >valgrind.
> >
> >Reviewed-by: Stefan Weil <sw@weilnetz.de>
> 
> Please hold off, I'm about to introduce large changes to qemu-nbd
> for asynchronous I/O (including getting rid of sharing_fds in favor
> of the main loop) and and I'd rather avoid gratuitous conflicts.

Okay, let's wait for Paolo's changes.

Stefan

Patch

diff --git a/qemu-nbd.c b/qemu-nbd.c
index 291cba2..ab7fa6c 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -500,8 +500,10 @@  int main(int argc, char **argv)
         sharing_fds[0] = tcp_socket_incoming(bindto, port);
     }
 
-    if (sharing_fds[0] == -1)
+    if (sharing_fds[0] == -1) {
+        g_free(sharing_fds);
         return 1;
+    }
 
     if (device) {
         int ret;