From ab6de3ae02b34ffd05ce6d6862856df5c47cf48d Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 29 May 2014 15:36:20 +0200 Subject: [PATCH] feature #2858: Fix minor memory leaks --- include/AddressRangePool.h | 6 ++--- include/Datastore.h | 2 +- include/VirtualNetwork.h | 10 +++++++- src/datastore/Datastore.cc | 5 ++++ src/nebula/NebulaTemplate.cc | 7 ++++-- src/vnm/AddressRangePool.cc | 34 ++++++--------------------- src/vnm/VirtualNetwork.cc | 45 +++++++++++++++++++++++++----------- 7 files changed, 62 insertions(+), 47 deletions(-) diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index eb64b5002f..6ad8265b83 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -43,13 +43,13 @@ public: // ************************************************************************* /** - * Builds the address range set from an array of VectorAttributes. This - * function is used to create address ranges. + * Builds the address range from a VectorAttribute. This function is used + * to create address ranges. * @param ars the vector of address ranges * @param error_msg describing the error * @return 0 on success */ - int from_vattr(vector ars, string& error_msg); + int from_vattr(VectorAttribute * ar, string& error_msg); /** * Builds the address range set from its XML representation. This function diff --git a/include/Datastore.h b/include/Datastore.h index d18b404dd3..fe2cc717f7 100644 --- a/include/Datastore.h +++ b/include/Datastore.h @@ -265,7 +265,7 @@ private: int cluster_id, const string& cluster_name); - virtual ~Datastore(){}; + virtual ~Datastore(); // ************************************************************************* // DataBase implementation (Private) diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 24baa6fcdd..c5355ec3aa 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -61,13 +61,21 @@ public: // ************************************************************************* /** - * Add an address range to the virtual network + * Add a set of address ranges to the virtual network * @param ars_tmpl template in the form AR = [TYPE=...,IP=...,SIZE=...]. * @param error_msg If the action fails, this message contains the reason. * @return 0 on success */ int add_ar(VirtualNetworkTemplate * ars_tmpl, string& error_msg); + /** + * Adds a set of address ranges + * @param var a vector of address ranges + * @param error_msg If the action fails, this message contains the reason. + * @return 0 on success + */ + int add_var(vector &var, string& error_msg); + /** * Removes an address range from the VNET * @param ar_id of the address range diff --git a/src/datastore/Datastore.cc b/src/datastore/Datastore.cc index ebcb869414..58018ef2eb 100644 --- a/src/datastore/Datastore.cc +++ b/src/datastore/Datastore.cc @@ -68,6 +68,11 @@ Datastore::Datastore( group_u = 1; } +Datastore::~Datastore() +{ + delete obj_template; +}; + /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ diff --git a/src/nebula/NebulaTemplate.cc b/src/nebula/NebulaTemplate.cc index a3a9617730..7d966dd98c 100644 --- a/src/nebula/NebulaTemplate.cc +++ b/src/nebula/NebulaTemplate.cc @@ -29,7 +29,7 @@ int NebulaTemplate::load_configuration() string aname; Attribute * attr; - map::iterator iter, j; + map::iterator iter, j, prev; set_conf_default(); @@ -59,7 +59,10 @@ int NebulaTemplate::load_configuration() else { delete iter->second; - conf_default.erase(iter++); + + prev = iter++; + + conf_default.erase(prev); } } diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index dc33a56db2..0aad359b0f 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -35,39 +35,19 @@ AddressRangePool::~AddressRangePool() /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int AddressRangePool::from_vattr(vector ars, string& error_msg) +int AddressRangePool::from_vattr(VectorAttribute* va, string& error_msg) { - vector::iterator it; + AddressRange * ar = new AddressRange(next_ar); - /* -------------------- Init the AR pool ---------------------------- */ - - for (it = ars.begin(); it != ars.end(); it++) + if (ar->from_vattr(va, error_msg) != 0) { - VectorAttribute * va = static_cast(*it); - - if (va == 0) - { - error_msg = "Wrong AR format"; - return -1; - } - - AddressRange * ar = new AddressRange(next_ar); - - if (ar->from_vattr(va, error_msg) != 0) - { - delete ar; - return -1; - } - - ar_pool.insert(make_pair(next_ar++, ar)); + delete ar; + return -1; } - /* ---------------------- Template ---------------------------------- */ + ar_pool.insert(make_pair(next_ar++, ar)); - for (it = ars.begin(); it != ars.end(); it++) - { - ar_template.set(*it); - } + ar_template.set(va); return 0; } diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index abd67031e5..2b591d42cb 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -167,7 +167,7 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str) remove_template_attribute("AR", ars); - if (ar_pool.from_vattr(ars, error_str) != 0) + if (add_var(ars, error_str) != 0) { goto error_ar; } @@ -587,23 +587,42 @@ int VirtualNetwork::nic_attribute( /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualNetwork::add_ar(VirtualNetworkTemplate * ars_tmpl, string& error_msg) +int VirtualNetwork::add_var(vector &var, string& error_msg) { - vector tmp_ars; - vector ars; - - vector::iterator it; - - ars_tmpl->get("AR", tmp_ars); - - for (it=tmp_ars.begin(); it!=tmp_ars.end(); it++) + for (vector::iterator it=var.begin(); it!=var.end(); it++) { - VectorAttribute * var = static_cast(*it); + VectorAttribute * oar = static_cast(*it); - ars.push_back(var->clone()); + if (oar == 0) + { + error_msg = "Invalid format for address range"; + return -1; + } + + VectorAttribute * ar = oar->clone(); + + if (ar_pool.from_vattr(ar, error_msg) != 0) + { + delete ar; + return -1; + } } - return ar_pool.from_vattr(ars, error_msg); + return 0; +} + +/* -------------------------------------------------------------------------- */ + +int VirtualNetwork::add_ar(VirtualNetworkTemplate * ars_tmpl, string& error_msg) +{ + vector var; + + if (ars_tmpl->get("AR", var) > 0) + { + return add_var(var, error_msg); + } + + return 0; } /* -------------------------------------------------------------------------- */