debuggers.hg

changeset 21955:d7ec0e180601

xl: fix memory leaks in xl create

Found using "valgrind xl create -n ..." and "valgrind xl create -e ..."

freeing config_data solves:
==18276== 944 bytes in 1 blocks are definitely lost in loss record 12 of 12
==18276== at 0x4022F0A: malloc (vg_replace_malloc.c:236)
==18276== by 0x404AEC1: libxl_read_file_contents (libxl_utils.c:258)
==18276== by 0x8056865: create_domain (xl_cmdimpl.c:1314)
==18276== by 0x8057E2D: main_create (xl_cmdimpl.c:3135)
==18276== by 0x804B2FB: main (xl.c:76)
==18276==

Adding free_domain_config() solves the following (plus presumably others
which didn't trigger because I have no devices of that type).

d_config->disks:
==18276== 61 (32 direct, 29 indirect) bytes in 1 blocks are definitely lost in loss record 9 of 12
==18276== at 0x4022F0A: malloc (vg_replace_malloc.c:236)
==18276== by 0x4022F94: realloc (vg_replace_malloc.c:525)
==18276== by 0x804E2D3: parse_config_data (xl_cmdimpl.c:715)
==18276== by 0x8056A7C: create_domain (xl_cmdimpl.c:1347)
==18276== by 0x8057E2D: main_create (xl_cmdimpl.c:3135)
==18276== by 0x804B2FB: main (xl.c:76)

d_config->vifs:
==18276== 76 (48 direct, 28 indirect) bytes in 1 blocks are definitely lost in loss record 10 of 12
==18276== at 0x4022F0A: malloc (vg_replace_malloc.c:236)
==18276== by 0x4022F94: realloc (vg_replace_malloc.c:525)
==18276== by 0x804E665: parse_config_data (xl_cmdimpl.c:779)
==18276== by 0x8056A7C: create_domain (xl_cmdimpl.c:1347)
==18276== by 0x8057E2D: main_create (xl_cmdimpl.c:3135)
==18276== by 0x804B2FB: main (xl.c:76)

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Ian Campbell <ian.campbell@citrix.com>
date Tue Aug 03 18:10:28 2010 +0100 (2010-08-03)
parents f6b91d2f9fa3
children 38aee6139719 b8a651785471
files tools/libxl/xl_cmdimpl.c
line diff
     1.1 --- a/tools/libxl/xl_cmdimpl.c	Tue Aug 03 18:09:21 2010 +0100
     1.2 +++ b/tools/libxl/xl_cmdimpl.c	Tue Aug 03 18:10:28 2010 +0100
     1.3 @@ -143,6 +143,16 @@ struct domain_config {
     1.4      enum action_on_shutdown on_crash;
     1.5  };
     1.6  
     1.7 +static void free_domain_config(struct domain_config *d_config)
     1.8 +{
     1.9 +    free(d_config->disks);
    1.10 +    free(d_config->vifs);
    1.11 +    free(d_config->vif2s);
    1.12 +    free(d_config->pcidevs);
    1.13 +    free(d_config->vfbs);
    1.14 +    free(d_config->vkbs);
    1.15 +}
    1.16 +
    1.17  /* Optional data, in order:
    1.18   *   4 bytes uint32_t  config file size
    1.19   *   n bytes           config file in Unix text file format
    1.20 @@ -1346,8 +1356,9 @@ static int create_domain(struct domain_c
    1.21  
    1.22      parse_config_data(config_file, config_data, config_len, &d_config, &dm_info);
    1.23  
    1.24 +    ret = 0;
    1.25      if (dom_info->dryrun)
    1.26 -        return 0;
    1.27 +        goto out;
    1.28  
    1.29      if (migrate_fd >= 0) {
    1.30          if (d_config.c_info.name) {
    1.31 @@ -1477,8 +1488,9 @@ start:
    1.32      if (!paused)
    1.33          libxl_domain_unpause(&ctx, domid);
    1.34  
    1.35 +    ret = domid; /* caller gets success in parent */
    1.36      if (!daemonize)
    1.37 -        return domid; /* caller gets success in parent */
    1.38 +        goto out;
    1.39  
    1.40      if (need_daemon) {
    1.41          char *fullname, *name;
    1.42 @@ -1605,6 +1617,12 @@ start:
    1.43  error_out:
    1.44      if (domid)
    1.45          libxl_domain_destroy(&ctx, domid, 0);
    1.46 +out:
    1.47 +
    1.48 +    free_domain_config(&d_config);
    1.49 +
    1.50 +    free(config_data);
    1.51 +
    1.52      return ret;
    1.53  }
    1.54  
    1.55 @@ -2143,6 +2161,7 @@ void list_domains_details(const libxl_do
    1.56          memset(&d_config, 0x00, sizeof(d_config));
    1.57          parse_config_data(config_file, (char *)data, len, &d_config, &dm_info);
    1.58          printf_info(info[i].domid, &d_config, &dm_info);
    1.59 +        free_domain_config(&d_config);
    1.60          free(data);
    1.61          free(config_file);
    1.62      }