From: Martin Kronbichler Date: Fri, 15 Apr 2011 14:24:18 +0000 (+0000) Subject: Fix memory leak in Polynomials::Hierarchical by using shared_ptr. X-Git-Tag: v8.0.0~4157 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0a2ce64b74ad43db49d9c7fdfa44a91b3487650b;p=dealii.git Fix memory leak in Polynomials::Hierarchical by using shared_ptr. git-svn-id: https://svn.dealii.org/trunk@23600 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/include/deal.II/base/polynomial.h b/deal.II/include/deal.II/base/polynomial.h index 5a302a5e60..449d1b9cd4 100644 --- a/deal.II/include/deal.II/base/polynomial.h +++ b/deal.II/include/deal.II/base/polynomial.h @@ -204,16 +204,16 @@ namespace Polynomials * Test for equality of two polynomials. */ bool operator == (const Polynomial & 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 void serialize (Archive & ar, const unsigned int version); @@ -605,7 +605,20 @@ namespace Polynomials static const std::vector & get_coefficients (const unsigned int p); - static std::vector *> 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 > > recursive_coefficients; }; } @@ -646,7 +659,7 @@ namespace Polynomials return value; } - + template diff --git a/deal.II/source/base/polynomial.cc b/deal.II/source/base/polynomial.cc index 47126aa09e..116b0b3aa5 100644 --- a/deal.II/source/base/polynomial.cc +++ b/deal.II/source/base/polynomial.cc @@ -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 - bool + bool Polynomial::operator == (const Polynomial & p) const { return (p.coefficients == coefficients); @@ -959,9 +959,8 @@ std::vector > Lobatto::generate_complete_basis (const unsigne // Reserve space for polynomials up to degree 19. Should be sufficient // for the start. - std::vector *> - Hierarchical::recursive_coefficients( - 20, static_cast*>(0)); + std::vector > > + Hierarchical::recursive_coefficients(20); @@ -996,7 +995,7 @@ std::vector > 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 > 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 >(c0); + recursive_coefficients[1] = + std_cxx1x::shared_ptr >(c1); } else if (k==2) { @@ -1035,7 +1036,8 @@ std::vector > 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 >(c2); } else { @@ -1078,7 +1080,8 @@ std::vector > 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 >(ck); }; }; } @@ -1094,16 +1097,9 @@ std::vector > 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 *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]; }