[mlpack-git] mlpack-1.0.x: Fix potential memory leak, and document known CosineTree bug. (a9f22b5)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 22:07:29 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : mlpack-1.0.x
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

>---------------------------------------------------------------

commit a9f22b58be3a82ee339040eb0251e1d7f92284be
Author: Ryan Curtin <ryan at ratml.org>
Date:   Wed Dec 10 15:31:30 2014 +0000

    Fix potential memory leak, and document known CosineTree bug.


>---------------------------------------------------------------

a9f22b58be3a82ee339040eb0251e1d7f92284be
 HISTORY.txt                                      |  3 +++
 src/mlpack/core/tree/cosine_tree/cosine_tree.cpp | 19 +++++++++++++++----
 src/mlpack/core/tree/cosine_tree/cosine_tree.hpp | 10 ++++++++--
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/HISTORY.txt b/HISTORY.txt
index a906829..4b1d853 100644
--- a/HISTORY.txt
+++ b/HISTORY.txt
@@ -21,6 +21,9 @@
   * Handle Newton method convergence better for
     SparseCoding::OptimizeDictionary() and make maximum iterations a parameter.
 
+  * Known bug: CosineTree construction may fail in some cases on i386 systems.
+    (#376)
+
 2014-08-29    mlpack 1.0.10
 
   * Bugfix for NeighborSearch regression which caused very slow allknn/allkfn.
diff --git a/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp b/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp
index 9ac498a..3752017 100644
--- a/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp
+++ b/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp
@@ -29,8 +29,8 @@ namespace tree {
 CosineTree::CosineTree(const arma::mat& dataset) :
     dataset(dataset),
     parent(NULL),
-    right(NULL),
     left(NULL),
+    right(NULL),
     numColumns(dataset.n_cols)
 {
   // Initialize sizes of column indices and l2 norms.
@@ -58,8 +58,8 @@ CosineTree::CosineTree(CosineTree& parentNode,
                        const std::vector<size_t>& subIndices) :
     dataset(parentNode.GetDataset()),
     parent(&parentNode),
-    right(NULL),
     left(NULL),
+    right(NULL),
     numColumns(subIndices.size())
 {
   // Initialize sizes of column indices and l2 norms.
@@ -87,7 +87,9 @@ CosineTree::CosineTree(const arma::mat& dataset,
                        const double delta) :
     dataset(dataset),
     epsilon(epsilon),
-    delta(delta)
+    delta(delta),
+    left(NULL),
+    right(NULL)
 {
   // Declare the cosine tree priority queue.
   CosineNodeQueue treeQueue;
@@ -102,7 +104,7 @@ CosineTree::CosineTree(const arma::mat& dataset,
   // Initialize Monte Carlo error estimate for comparison.
   double monteCarloError = root.FrobNormSquared();
 
-  while(monteCarloError > epsilon * root.FrobNormSquared())
+  while (monteCarloError > epsilon * root.FrobNormSquared())
   {
     // Pop node from queue with highest projection error.
     CosineTree* currentNode;
@@ -144,6 +146,15 @@ CosineTree::CosineTree(const arma::mat& dataset,
   ConstructBasis(treeQueue);
 }
 
+CosineTree::~CosineTree()
+{
+  // Clean the memory.
+  if (left)
+    delete left;
+  if (right)
+    delete right;
+}
+
 void CosineTree::ModifiedGramSchmidt(CosineNodeQueue& treeQueue,
                                      arma::vec& centroid,
                                      arma::vec& newBasisVector,
diff --git a/src/mlpack/core/tree/cosine_tree/cosine_tree.hpp b/src/mlpack/core/tree/cosine_tree/cosine_tree.hpp
index 63a94b2..6402e73 100644
--- a/src/mlpack/core/tree/cosine_tree/cosine_tree.hpp
+++ b/src/mlpack/core/tree/cosine_tree/cosine_tree.hpp
@@ -81,6 +81,12 @@ class CosineTree
              const double delta);
 
   /**
+   * Destroy the cosine tree and all of its children (take care of the memory
+   * allocations too).
+   */
+  ~CosineTree();
+
+  /**
    * Calculates the orthonormalization of the passed centroid, with respect to
    * the current vector subspace.
    *
@@ -222,10 +228,10 @@ class CosineTree
   arma::mat basis;
   //! Parent of the node.
   CosineTree* parent;
-  //! Right child of the node.
-  CosineTree* right;
   //! Left child of the node.
   CosineTree* left;
+  //! Right child of the node.
+  CosineTree* right;
   //! Indices of columns of input matrix in the node.
   std::vector<size_t> indices;
   //! L2-norm squared of columns in the node.



More information about the mlpack-git mailing list