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.
pull/36/head
Tatsuhiro Tsujikawa 2012-12-08 17:52:10 +09:00
parent a8641b0998
commit 9d7bb9d01a
5 changed files with 31 additions and 1 deletions

View File

@ -347,4 +347,9 @@ const SharedHandle<OptionParser>& OptionParser::getInstance()
return optionParser_;
}
void OptionParser::deleteInstance()
{
optionParser_.reset();
}
} // namespace aria2

View File

@ -94,6 +94,9 @@ public:
const OptionHandler* findByShortName(char shortName) const;
static const SharedHandle<OptionParser>& getInstance();
// Deletes statically allocated instace. Call this at the end of the
// program.
static void deleteInstance();
};
} // namespace aria2

View File

@ -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;
}

View File

@ -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
/**

View File

@ -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
/**