summaryrefslogtreecommitdiff
path: root/src/mongo/unittest/unittest.cpp
diff options
context:
space:
mode:
authorAndrew Morrow <acm@mongodb.com>2014-10-23 15:57:12 -0400
committerAndrew Morrow <acm@mongodb.com>2014-10-27 11:07:37 -0400
commitc1c1116c488351e0cdbd7626dcc8ce1f5dc828af (patch)
tree7ed0e6596b9ea93715f23dd112c251a03bb40c28 /src/mongo/unittest/unittest.cpp
parent0fde30ad2f873e7f835aa224e83a97ae6d4a171e (diff)
downloadmongo-c1c1116c488351e0cdbd7626dcc8ce1f5dc828af.tar.gz
SERVER-15707 Fix leaks in C++ unit test framework
Diffstat (limited to 'src/mongo/unittest/unittest.cpp')
-rw-r--r--src/mongo/unittest/unittest.cpp31
1 files changed, 19 insertions, 12 deletions
diff --git a/src/mongo/unittest/unittest.cpp b/src/mongo/unittest/unittest.cpp
index 030014094b3..be34f4207b0 100644
--- a/src/mongo/unittest/unittest.cpp
+++ b/src/mongo/unittest/unittest.cpp
@@ -52,7 +52,8 @@ namespace mongo {
namespace {
logger::MessageLogDomain* unittestOutput =
logger::globalLogManager()->getNamedDomain("unittest");
- typedef std::map<std::string, Suite*> SuiteMap;
+
+ typedef std::map<std::string, boost::shared_ptr<Suite> > SuiteMap;
inline SuiteMap& _allSuites() {
static SuiteMap allSuites;
@@ -188,7 +189,7 @@ namespace {
Suite::~Suite() {}
void Suite::add(const std::string& name, const TestFunction& testFn) {
- _tests.push_back(new TestHolder(name, testFn));
+ _tests.push_back(boost::shared_ptr<TestHolder>(new TestHolder(name, testFn)));
}
Result * Suite::run( const std::string& filter, int runsPerTest ) {
@@ -201,8 +202,8 @@ namespace {
Result * r = new Result( _name );
Result::cur = r;
- for ( std::vector<TestHolder*>::iterator i=_tests.begin(); i!=_tests.end(); i++ ) {
- TestHolder* tc = *i;
+ for ( std::vector<boost::shared_ptr<TestHolder>>::iterator i=_tests.begin(); i!=_tests.end(); i++ ) {
+ boost::shared_ptr<TestHolder>& tc = *i;
if ( filter.size() && tc->getName().find( filter ) == std::string::npos ) {
LOG(1) << "\t skipping test: " << tc->getName() << " because doesn't match filter" << std::endl;
continue;
@@ -282,7 +283,7 @@ namespace {
for ( std::vector<std::string>::iterator i=torun.begin(); i!=torun.end(); i++ ) {
std::string name = *i;
- Suite* s = _allSuites()[name];
+ boost::shared_ptr<Suite>& s = _allSuites()[name];
fassert( 16145, s );
log() << "going to run suite: " << name << std::endl;
@@ -300,6 +301,7 @@ namespace {
Result totals ("TOTALS");
std::vector<std::string> failedSuites;
+ Result::cur = NULL;
for ( std::vector<Result*>::iterator i=results.begin(); i!=results.end(); i++ ) {
Result* r = *i;
log() << r->toString();
@@ -317,6 +319,8 @@ namespace {
}
asserts += r->_asserts;
millis += r->_millis;
+
+ delete r;
}
totals._tests = tests;
@@ -344,16 +348,19 @@ namespace {
}
void Suite::registerSuite( const std::string& name , Suite* s ) {
- Suite*& m = _allSuites()[name];
- fassert( 10162, ! m );
- m = s;
+ boost::shared_ptr<Suite>& m = _allSuites()[name];
+ fassert( 10162, !m );
+ m.reset(s);
}
Suite* Suite::getSuite(const std::string& name) {
- Suite* result = _allSuites()[name];
- if (!result)
- result = new Suite(name); // Suites are self-registering.
- return result;
+ boost::shared_ptr<Suite>& result = _allSuites()[name];
+ if (!result) {
+ // Suites are self-registering.
+ new Suite(name);
+ }
+ invariant(result);
+ return result.get();
}
void Suite::setupTests() {}