]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Fix memory leak in Polynomials::Hierarchical by using shared_ptr.
authorkronbichler <kronbichler@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 15 Apr 2011 14:24:18 +0000 (14:24 +0000)
committerkronbichler <kronbichler@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 15 Apr 2011 14:24:18 +0000 (14:24 +0000)
git-svn-id: https://svn.dealii.org/trunk@23600 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/include/deal.II/base/polynomial.h
deal.II/source/base/polynomial.cc

index 5a302a5e6077c80c5b63c052bc7a5ae74940f534..449d1b9cd40b6a84379cc389a0248f760a12356c 100644 (file)
@@ -204,16 +204,16 @@ namespace Polynomials
                                                        *  Test for equality of two polynomials.
                                                        */
       bool operator == (const Polynomial<number> & p)  const;
-    
+
                                        /**
                                         * Print coefficients.
                                         */
       void print(std::ostream& out) const;
-      
+
                                        /**
-                                        * Write or read the data of this object to or 
+                                        * Write or read the data of this object to or
                                         * from a stream for the purpose of serialization.
-                                        */ 
+                                        */
       template <class Archive>
       void serialize (Archive & ar, const unsigned int version);
 
@@ -605,7 +605,20 @@ namespace Polynomials
      static const std::vector<double> &
      get_coefficients (const unsigned int p);
 
-     static std::vector<const std::vector<double> *> recursive_coefficients;
+                                       /**
+                                        * Vector with already computed
+                                        * coefficients. For each degree of the
+                                        * polynomial, we keep one pointer to
+                                        * the list of coefficients; we do so
+                                        * rather than keeping a vector of
+                                        * vectors in order to simplify
+                                        * programming multithread-safe. In
+                                        * order to avoid memory leak, we use a
+                                        * shared_ptr in order to correctly
+                                        * free the memory of the vectors when
+                                        * the global destructor is called.
+                                        */
+    static std::vector<std_cxx1x::shared_ptr<const std::vector<double> > > recursive_coefficients;
    };
 }
 
@@ -646,7 +659,7 @@ namespace Polynomials
 
     return value;
   }
-  
+
 
 
   template <typename number>
index 47126aa09e7c674bff66708b63bd1a6b75b5f17e..116b0b3aa5eec9e14bdec9d806210bdb1ec8a200 100644 (file)
@@ -2,7 +2,7 @@
 //      $Id$
 //    Version: $Name$
 //
-//    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2010 by the deal.II authors
+//    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2010, 2011 by the deal.II authors
 //
 //    This file is subject to QPL and may not be  distributed
 //    without copyright and license information. Please refer
@@ -211,7 +211,7 @@ namespace Polynomials
   }
 
   template <typename number >
-  bool 
+  bool
   Polynomial<number>::operator == (const Polynomial<number> & p)  const
   {
     return (p.coefficients == coefficients);
@@ -959,9 +959,8 @@ std::vector<Polynomial<double> > Lobatto::generate_complete_basis (const unsigne
 
 // Reserve space for polynomials up to degree 19. Should be sufficient
 // for the start.
-  std::vector<const std::vector<double> *>
-  Hierarchical::recursive_coefficients(
-     20, static_cast<const std::vector<double>*>(0));
+  std::vector<std_cxx1x::shared_ptr<const std::vector<double> > >
+  Hierarchical::recursive_coefficients(20);
 
 
 
@@ -996,7 +995,7 @@ std::vector<Polynomial<double> > Lobatto::generate_complete_basis (const unsigne
                                            // no, then generate the
                                            // respective coefficients
       {
-        recursive_coefficients.resize (k+1, 0);
+        recursive_coefficients.resize (k+1);
 
         if (k<=1)
           {
@@ -1018,8 +1017,10 @@ std::vector<Polynomial<double> > Lobatto::generate_complete_basis (const unsigne
 
                                              // now make these arrays
                                              // const
-            recursive_coefficients[0] = c0;
-            recursive_coefficients[1] = c1;
+            recursive_coefficients[0] =
+             std_cxx1x::shared_ptr<const std::vector<double> >(c0);
+            recursive_coefficients[1] =
+             std_cxx1x::shared_ptr<const std::vector<double> >(c1);
           }
         else if (k==2)
           {
@@ -1035,7 +1036,8 @@ std::vector<Polynomial<double> > Lobatto::generate_complete_basis (const unsigne
             (*c2)[1] =  -4.*a;
             (*c2)[2] =   4.*a;
 
-            recursive_coefficients[2] = c2;
+            recursive_coefficients[2] =
+             std_cxx1x::shared_ptr<const std::vector<double> >(c2);
           }
         else
           {
@@ -1078,7 +1080,8 @@ std::vector<Polynomial<double> > Lobatto::generate_complete_basis (const unsigne
                                              // created vector to the
                                              // const pointer in the
                                              // coefficients array
-            recursive_coefficients[k] = ck;
+            recursive_coefficients[k] =
+             std_cxx1x::shared_ptr<const std::vector<double> >(ck);
           };
       };
   }
@@ -1094,16 +1097,9 @@ std::vector<Polynomial<double> > Lobatto::generate_complete_basis (const unsigne
 
                                    // then get a pointer to the array
                                    // of coefficients. do that in a MT
-                                     // safe way
-    coefficients_lock.acquire ();
-    const std::vector<double> *p = recursive_coefficients[k];
-    coefficients_lock.release ();
-
-                                   // return the object pointed
-                                   // to. since this object does not
-                                   // change any more once computed,
-                                   // this is MT safe
-    return *p;
+                                  // safe way
+    Threads::ThreadMutex::ScopedLock lock (coefficients_lock);
+    return *recursive_coefficients[k];
   }
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.