Hello Anand and David,

I use the tool valgrind to whether there exists memory leak:

valgrind  --tool=memcheck  --trace-origins=yes --leak-chek=full btrfs sub list 
/mnt
There still exists memory leak related to open_file_or_dir() (After applying 
this patch).

I guess it is because we should call closedir(dirstram) rather than close(fd) 
if opening a directory.
Maybe we can make open_file_or_dir() something like:

open_file_or_dir(char *name, DIR **dirstream)

if we opened a directory, we close it by calling closeidr(dirstream).
elsewise, close(fd) is ok!..

I am wondering why close(fd) can not just finish this work……


Thanks,
Wang
> From: Wang Shilong <wangsl.f...@cn.fujitsu.com>
> 
> After calling opendir() successfully, closedir() should be
> also called to free memory. Otherwise, memory leak happens.
> 
> Signed-off-by: Anand Jain <anand.j...@oracle.com>
> Signed-off-by: Wang Shilong <wangsl.f...@cn.fujitsu.com>
> Reviewed-by: Anand Jain <anand.j...@oracle.com>
> ---
> utils.c |    6 ++++--
> 1 files changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/utils.c b/utils.c
> index d3bec9b..4b3c778 100644
> --- a/utils.c
> +++ b/utils.c
> @@ -1503,7 +1503,7 @@ int open_file_or_dir(const char *fname)
> {
>       int ret;
>       struct stat st;
> -     DIR *dirstream;
> +     DIR *dirstream = NULL;
>       int fd;
> 
>       ret = stat(fname, &st);
> @@ -1520,7 +1520,9 @@ int open_file_or_dir(const char *fname)
>               fd = open(fname, O_RDWR);
>       }
>       if (fd < 0) {
> -             return -3;
> +             fd = -3;
> +             if (dirstream)
> +                     closedir(dirstream);
>       }
>       return fd;
> }
> -- 
> 1.7.7.6
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majord...@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to