From d0b7e864be07f9e293b9e8719c07b73a0d92ff64 Mon Sep 17 00:00:00 2001 From: Binh-Minh Ribler Date: Sat, 10 Oct 2015 23:44:03 -0500 Subject: [svn-r28030] Purpose: Fix memory leaks Description: Merged from trunk r28027. - Removed H5Library::instance because it is unnecessary. All H5Library's methods are static. This, in turn, removed the memory leaks by H5Library::instance not being deleted. - Added ObjCreatPropList::deleteConstants to atexist() list - Cleaned up comments and format inconsistencies with 1.8 Platforms tested: Linux/32 2.6 (jam) Linux/64 (platypus) Darwin (osx1010test) --- c++/src/H5IdComponent.cpp | 16 ++++++---- c++/src/H5IdComponent.h | 32 ++++++++++---------- c++/src/H5Library.cpp | 74 +++++++++++++++++++---------------------------- c++/src/H5Library.h | 7 +---- 4 files changed, 57 insertions(+), 72 deletions(-) diff --git a/c++/src/H5IdComponent.cpp b/c++/src/H5IdComponent.cpp index 93ee4fd..19d68cf 100644 --- a/c++/src/H5IdComponent.cpp +++ b/c++/src/H5IdComponent.cpp @@ -26,14 +26,19 @@ namespace H5 { #endif -// This flag controls whether H5Library::initH5cpp has been called to register -// terminating functions with atexit() +// This flag indicates whether H5Library::initH5cpp has been called to register +// the terminating functions with atexit() bool IdComponent::H5cppinit = false; + +// This flag is used to decide whether H5dont_atexit should be called. +// Subclasses that have global constants use it. This is a temporary +// work-around in 1.8.16. It will be removed after HDFFV-9540 is fixed. bool IdComponent::H5dontAtexit_called = false; //-------------------------------------------------------------------------- // Function: IdComponent overloaded constructor -// Purpose Creates an IdComponent object using the id of an existing object. +///\brief Creates an IdComponent object using the id of an existing +/// object. - Obsolete, will be removed in 1.8.17 // Param h5_id - IN: Id of an existing object // Exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 @@ -43,7 +48,6 @@ bool IdComponent::H5dontAtexit_called = false; // been moved to the sub-classes. It will be removed in 1.10 release. If its // removal does not raise any problems in 1.10, it will be removed from 1.8 in // subsequent releases. -// - Removed from documentation in 1.8.16 -BMR (October 2015) //-------------------------------------------------------------------------- IdComponent::IdComponent(const hid_t h5_id) {} @@ -295,10 +299,10 @@ H5std_string IdComponent::inMemFunc(const char* func_name) const IdComponent::IdComponent() { // initH5cpp will register the terminating functions with atexit(). - // We only do this once. + // This should only be done once. if (!H5cppinit) { - H5Library::getInstance()->initH5cpp(); + H5Library::initH5cpp(); H5cppinit = true; } } diff --git a/c++/src/H5IdComponent.h b/c++/src/H5IdComponent.h index 4b72cdf..34f2da8 100644 --- a/c++/src/H5IdComponent.h +++ b/c++/src/H5IdComponent.h @@ -32,12 +32,7 @@ class DataSpace; rarely needs them. */ class H5_DLLCPP IdComponent { - public: - -#ifndef DOXYGEN_SHOULD_SKIP_THIS - static bool H5cppinit; - static bool H5dontAtexit_called; -#endif // DOXYGEN_SHOULD_SKIP_THIS + public: // Increment reference counter. void incRefCount(const hid_t obj_id) const; @@ -60,11 +55,6 @@ class H5_DLLCPP IdComponent { // Assignment operator. IdComponent& operator=( const IdComponent& rhs ); -#ifndef DOXYGEN_SHOULD_SKIP_THIS - // Gets the identifier of this object. - virtual hid_t getId () const = 0; -#endif // DOXYGEN_SHOULD_SKIP_THIS - // Sets the identifier of this object to a new value. void setId(const hid_t new_id); @@ -78,10 +68,14 @@ class H5_DLLCPP IdComponent { // Creates an object to hold an HDF5 identifier. IdComponent( const hid_t h5_id ); +#ifndef DOXYGEN_SHOULD_SKIP_THIS + // Copy constructor: makes copy of the original IdComponent object. - // IdComponent( const IdComponent& original ); + // IdComponent( const IdComponent& original ); - removed from 1.8.15 + + // Gets the identifier of this object. + virtual hid_t getId () const = 0; -#ifndef DOXYGEN_SHOULD_SKIP_THIS // Pure virtual function for there are various H5*close for the // subclasses. virtual void close() = 0; @@ -99,7 +93,8 @@ class H5_DLLCPP IdComponent { virtual ~IdComponent(); #ifndef DOXYGEN_SHOULD_SKIP_THIS - protected: + + protected: // Default constructor. IdComponent(); @@ -113,7 +108,14 @@ class H5_DLLCPP IdComponent { // Sets the identifier of this object to a new value. - this one // doesn't increment reference count virtual void p_setId(const hid_t new_id) = 0; - //virtual void p_setId(const hid_t new_id); + + // This flag is used to decide whether H5dont_atexit should be called + static bool H5dontAtexit_called; + + private: + // This flag indicates whether H5Library::initH5cpp has been called + // to register various terminating functions with atexit() + static bool H5cppinit; #endif // DOXYGEN_SHOULD_SKIP_THIS diff --git a/c++/src/H5Library.cpp b/c++/src/H5Library.cpp index 552f276..40c766a 100644 --- a/c++/src/H5Library.cpp +++ b/c++/src/H5Library.cpp @@ -38,17 +38,14 @@ namespace H5 { #endif #ifndef DOXYGEN_SHOULD_SKIP_THIS -// This static variable will be set to true when dontAtExit is called -// - unused, will be removed in future releases. + +// This static variable is unused, will be removed in future releases. bool H5Library::need_cleanup = false; -// IdComponent default constructor instantiates this object to register cleanup -// functions before any global constant is created. -H5Library* H5Library::instance = 0; #endif // DOXYGEN_SHOULD_SKIP_THIS //-------------------------------------------------------------------------- -// Function: H5Library::open +// Function: H5Library::open (static) ///\brief Initializes the HDF5 library. /// ///\exception H5::LibraryIException @@ -64,7 +61,7 @@ void H5Library::open() } //-------------------------------------------------------------------------- -// Function: H5Library::close +// Function: H5Library::close (static) ///\brief Flushes all data to disk, closes files, and cleans up memory. /// ///\exception H5::LibraryIException @@ -80,7 +77,7 @@ void H5Library::close() } //-------------------------------------------------------------------------- -// Function: H5Library::dontAtExit +// Function: H5Library::dontAtExit (static) ///\brief Instructs library not to install the C \c atexit cleanup routine /// ///\exception H5::LibraryIException @@ -95,7 +92,7 @@ void H5Library::dontAtExit() } //-------------------------------------------------------------------------- -// Function: H5Library::getLibVersion +// Function: H5Library::getLibVersion (static) ///\brief Returns the HDF library release number. ///\param majnum - OUT: Major version of the library ///\param minnum - OUT: Minor version of the library @@ -113,7 +110,7 @@ void H5Library::getLibVersion( unsigned& majnum, unsigned& minnum, unsigned& rel } //-------------------------------------------------------------------------- -// Function: H5Library::checkVersion +// Function: H5Library::checkVersion (static) ///\brief Verifies that the arguments match the version numbers /// compiled into the library ///\param majnum - IN: Major version of the library @@ -136,7 +133,7 @@ void H5Library::checkVersion(unsigned majnum, unsigned minnum, unsigned relnum) } //-------------------------------------------------------------------------- -// Function: H5Library::garbageCollect +// Function: H5Library::garbageCollect (static) ///\brief Walks through all the garbage collection routines for the /// library, which are supposed to free any unused memory they /// have allocated. @@ -165,7 +162,7 @@ void H5Library::garbageCollect() } //-------------------------------------------------------------------------- -// Function: H5Library::initH5cpp +// Function: H5Library::initH5cpp (static) ///\brief Initializes C++ library and registers terminating functions at /// exit. Only for the library functions, not for user-defined /// functions. @@ -173,52 +170,56 @@ void H5Library::garbageCollect() // initH5cpp registers the following functions with std::atexit(): // termH5cpp() - calls H5close() after all cleanup in // the C++ library is done -// ::deleteConstants - deletes all references for -// global constants +// ::deleteConstants - deletes all references +// for global constants ///\exception H5::LibraryIException // // Programmer Binh-Minh Ribler - September, 2015 //-------------------------------------------------------------------------- void H5Library::initH5cpp() { - // Register terminating functions with atexit(); they will be invoked in the - // reversed order + // Register terminating functions with atexit(); they will be invoked in + // the reversed order int ret_value = 0; ret_value = std::atexit(termH5cpp); if (ret_value != 0) - throw LibraryIException("H5Library::initH5cpp", "Registration of termH5cpp failed"); + throw LibraryIException("H5Library::initH5cpp", "Registrating termH5cpp failed"); ret_value = std::atexit(PredType::deleteConstants); if (ret_value != 0) - throw LibraryIException("H5Library::initH5cpp", "Registration of PredType::deleteConstants failed"); + throw LibraryIException("H5Library::initH5cpp", "Registrating PredType::deleteConstants failed"); ret_value = std::atexit(PropList::deleteConstants); if (ret_value != 0) - throw LibraryIException("H5Library::initH5cpp", "Registration of PropList::deleteConstants failed"); + throw LibraryIException("H5Library::initH5cpp", "Registrating PropList::deleteConstants failed"); ret_value = std::atexit(FileAccPropList::deleteConstants); if (ret_value != 0) - throw LibraryIException("H5Library::initH5cpp", "Registration of FileAccPropList::deleteConstants failed"); + throw LibraryIException("H5Library::initH5cpp", "Registrating FileAccPropList::deleteConstants failed"); ret_value = std::atexit(FileCreatPropList::deleteConstants); if (ret_value != 0) - throw LibraryIException("H5Library::initH5cpp", "Registration of FileCreatPropList::deleteConstants failed"); + throw LibraryIException("H5Library::initH5cpp", "Registrating FileCreatPropList::deleteConstants failed"); ret_value = std::atexit(DSetMemXferPropList::deleteConstants); if (ret_value != 0) - throw LibraryIException("H5Library::initH5cpp", "Registration of DSetMemXferPropList::deleteConstants failed"); + throw LibraryIException("H5Library::initH5cpp", "Registrating DSetMemXferPropList::deleteConstants failed"); ret_value = std::atexit(DSetCreatPropList::deleteConstants); if (ret_value != 0) - throw LibraryIException("H5Library::initH5cpp", "Registration of DSetCreatPropList::deleteConstants failed"); + throw LibraryIException("H5Library::initH5cpp", "Registrating DSetCreatPropList::deleteConstants failed"); + + ret_value = std::atexit(ObjCreatPropList::deleteConstants); + if (ret_value != 0) + throw LibraryIException("H5Library::initH5cpp", "Registrating ObjCreatPropList::deleteConstants failed"); ret_value = std::atexit(DataSpace::deleteConstants); if (ret_value != 0) - throw LibraryIException("H5Library::initH5cpp", "Registration of DataSpace::deleteConstants failed"); + throw LibraryIException("H5Library::initH5cpp", "Registrating DataSpace::deleteConstants failed"); } //-------------------------------------------------------------------------- -// Function: H5Library::termH5cpp +// Function: H5Library::termH5cpp (static) ///\brief Sends request for the C layer to terminate. ///\par Description /// If the C library fails to terminate, exit with a failure. @@ -233,24 +234,7 @@ void H5Library::termH5cpp() } //-------------------------------------------------------------------------- -// Function: H5Library::getInstance -///\brief Provides a way to instantiate the class. -///\par Description -/// getInstance ensures that only one instance of the H5Library -/// is created. -// Programmer Binh-Minh Ribler - September, 2015 -//-------------------------------------------------------------------------- -H5Library* H5Library::getInstance() -{ - if (H5Library::instance == 0) - { - instance = new H5Library(); - } - return(instance); -} - -//-------------------------------------------------------------------------- -// Function: H5Library::setFreeListLimits +// Function: H5Library::setFreeListLimits (static) ///\brief Sets limits on the different kinds of free lists. ///\param reg_global_lim - IN: Limit on all "regular" free list memory used ///\param reg_list_lim - IN: Limit on memory used in each "regular" free list @@ -277,10 +261,10 @@ void H5Library::setFreeListLimits(int reg_global_lim, int reg_list_lim, } } -// Default constructor - no instance ever created by outsiders +// Default constructor - private H5Library::H5Library(){}; -// Destructor +// Destructor - private H5Library::~H5Library(){}; #ifndef H5_NO_NAMESPACE diff --git a/c++/src/H5Library.h b/c++/src/H5Library.h index 5e94cb5..336f9c8 100644 --- a/c++/src/H5Library.h +++ b/c++/src/H5Library.h @@ -31,7 +31,7 @@ class H5_DLLCPP H5Library { public: #ifndef DOXYGEN_SHOULD_SKIP_THIS static bool need_cleanup; // indicates if H5close should be called - // - unused, will be removed in future releases. + // - unused, will be removed in future releases. #endif // DOXYGEN_SHOULD_SKIP_THIS // Initializes the HDF5 library. @@ -65,15 +65,10 @@ class H5_DLLCPP H5Library { // Sends request for terminating the HDF5 library. static void termH5cpp(void); - static H5Library* getInstance(); - #ifndef DOXYGEN_SHOULD_SKIP_THIS private: - // private instance to be created by H5Library only - static H5Library* instance; - // Default constructor - no instance ever created from outsiders H5Library(); -- cgit v0.12