Skip to content

Commit f452222

Browse files
committed
Avoid trivial memory leak in unit test driver
If we add only a subset of tests to the runner (via --re or --deny_re options), we're careful to move the other tests to a "rejects" suite so they'll get deleted there, but the "supertest", the suite holding all the other tests, wasn't getting deleted in those cases. This (on top of my earlier fixes, and using the MOOSE suppressions file for third-party library issues) gets selective valgrind runs of our unit tests clean for me.
1 parent 3936c68 commit f452222

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

tests/driver.C

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,17 @@ int main(int argc, char ** argv)
116116
std::string deny_regex_string = "^$";
117117
deny_regex_string = libMesh::command_line_next("--deny_re", deny_regex_string);
118118

119+
// We might have to delete the test suite ourselves, after the
120+
// runner has deleted whatever subtests it has.
121+
std::unique_ptr<CppUnit::Test> owned_suite;
122+
119123
// Recursively add tests matching the regex to the runner object.
120124
CppUnit::TextUi::TestRunner runner;
121125

122-
// The Cppunit registry object that knows about all the tests.
126+
// The Cppunit registry object that knows about all the tests, and
127+
// the test suite it creates.
123128
CppUnit::TestFactoryRegistry & registry = CppUnit::TestFactoryRegistry::getRegistry();
129+
CppUnit::Test * suite = registry.makeTest();
124130

125131
// A test suite container for holding tests not matching the regex. When main's
126132
// scope ends, this class's destructor will delete the rejected tests
@@ -134,15 +140,17 @@ int main(int argc, char ** argv)
134140
// Add all tests which match the re to the runner object.
135141
libMesh::out << "Will run the following tests:" << std::endl;
136142
const int n_tests_added =
137-
add_matching_tests_to_runner(registry.makeTest(),
143+
add_matching_tests_to_runner(suite,
138144
allow_regex_string, allow_regex,
139145
deny_regex_string, deny_regex,
140146
runner, rejects);
141147
if (n_tests_added >= 0)
142148
libMesh::out << "--- Running " << n_tests_added << " tests in total." << std::endl;
149+
if (n_tests_added != -12345)
150+
owned_suite.reset(suite);
143151
#else
144152
// If no C++11 <regex> just run all the tests.
145-
runner.addTest(registry.makeTest());
153+
runner.addTest(suite);
146154
#endif
147155

148156
std::unique_ptr<CppUnit::TestResult> controller;

0 commit comments

Comments
 (0)