From 9d7bb9d01af2c0ef1b748ac2e240b20439285fd4 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 8 Dec 2012 17:52:10 +0900 Subject: [PATCH] Release statically allocated resources for prefs and options Not realeasing these resources does not hurt the correctness of the program, but memory leak detection tool, such as valgrind, reports it as potential memory leak. It is better to distinguish it from the real ones. --- src/OptionParser.cc | 5 +++++ src/OptionParser.h | 3 +++ src/Platform.cc | 8 +++++++- src/prefs.cc | 12 ++++++++++++ src/prefs.h | 4 ++++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/OptionParser.cc b/src/OptionParser.cc index d2250374..157d74ec 100644 --- a/src/OptionParser.cc +++ b/src/OptionParser.cc @@ -347,4 +347,9 @@ const SharedHandle& OptionParser::getInstance() return optionParser_; } +void OptionParser::deleteInstance() +{ + optionParser_.reset(); +} + } // namespace aria2 diff --git a/src/OptionParser.h b/src/OptionParser.h index 617712db..2cd9f8e7 100644 --- a/src/OptionParser.h +++ b/src/OptionParser.h @@ -94,6 +94,9 @@ public: const OptionHandler* findByShortName(char shortName) const; static const SharedHandle& getInstance(); + // Deletes statically allocated instace. Call this at the end of the + // program. + static void deleteInstance(); }; } // namespace aria2 diff --git a/src/Platform.cc b/src/Platform.cc index a2749a4e..298294ae 100644 --- a/src/Platform.cc +++ b/src/Platform.cc @@ -73,6 +73,8 @@ #include "message.h" #include "fmt.h" #include "console.h" +#include "OptionParser.h" +#include "prefs.h" #ifdef HAVE_LIBGMP # include "a2gmp.h" #endif // HAVE_LIBGMP @@ -172,7 +174,11 @@ bool Platform::tearDown() #ifdef HAVE_WINSOCK2_H WSACleanup(); #endif // HAVE_WINSOCK2_H - + // Deletes statically allocated resources. This is done to + // distinguish memory leak from them. This is handly to use + // valgrind. + OptionParser::deleteInstance(); + option::deletePrefResource(); return true; } diff --git a/src/prefs.cc b/src/prefs.cc index 6675de2e..62089096 100644 --- a/src/prefs.cc +++ b/src/prefs.cc @@ -51,6 +51,13 @@ public: // We add special null pref whose ID is 0. makePref(""); } + ~PrefFactory() + { + for(size_t i = 0; i < count_; ++i) { + delete i2p_[i]; + } + } + size_t nextId() { return count_++; @@ -117,6 +124,11 @@ const Pref* k2p(const std::string& key) return getPrefFactory()->k2p(key); } +void deletePrefResource() +{ + delete getPrefFactory(); +} + } // namespace option /** diff --git a/src/prefs.h b/src/prefs.h index 01ed3397..ec15169c 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -60,6 +60,10 @@ const Pref* i2p(size_t id); // special null Pref whose ID is 0. const Pref* k2p(const std::string& k); +// Deletes resources allocated for preferences. Call this function at +// the end of the program only once. +void deletePrefResource(); + } // namespace option /**