Valgrind: Fixed memory leaks when running testrunner that was found by Valgrind

This commit is contained in:
Daniel Marjamäki 2009-12-28 19:48:30 +01:00
parent 61e61c1584
commit d3d3ad101f
2 changed files with 42 additions and 13 deletions

View File

@ -56,6 +56,16 @@ bool ExecutionPath::parseCondition(const Token &tok, std::list<ExecutionPath *>
} }
static void dealloc_checks(std::list<ExecutionPath *> &checks)
{
while (!checks.empty())
{
delete checks.back();
checks.pop_back();
}
}
static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPath *> &checks) static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPath *> &checks)
{ {
const std::auto_ptr<ExecutionPath> check(checks.front()->copy()); const std::auto_ptr<ExecutionPath> check(checks.front()->copy());
@ -100,12 +110,8 @@ static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPa
// parse condition // parse condition
if (checks.size() > 10 || check->parseCondition(*tok->next(), checks)) if (checks.size() > 10 || check->parseCondition(*tok->next(), checks))
{ {
while (!checks.empty()) dealloc_checks(checks);
{ dealloc_checks(newchecks);
delete checks.back();
checks.pop_back();
}
return 0; return 0;
} }
@ -116,7 +122,11 @@ static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPa
tok = tok ? tok->next() : 0; tok = tok ? tok->next() : 0;
if (!Token::simpleMatch(tok, "{")) if (!Token::simpleMatch(tok, "{"))
{
dealloc_checks(checks);
dealloc_checks(newchecks);
return 0; return 0;
}
// Recursively check into the if .. // Recursively check into the if ..
{ {
@ -126,7 +136,11 @@ static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPa
c.push_back((*it)->copy()); c.push_back((*it)->copy());
const Token *tokerr = checkExecutionPaths_(tok->next(), c); const Token *tokerr = checkExecutionPaths_(tok->next(), c);
if (tokerr) if (tokerr)
{
dealloc_checks(c);
dealloc_checks(newchecks);
return tokerr; return tokerr;
}
while (!c.empty()) while (!c.empty())
{ {
newchecks.push_back(c.back()); newchecks.push_back(c.back());
@ -149,16 +163,22 @@ static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPa
// there is no "if".. // there is no "if"..
const Token *tokerr = checkExecutionPaths_(tok->next(), checks); const Token *tokerr = checkExecutionPaths_(tok->next(), checks);
if (tokerr) if (tokerr)
{
dealloc_checks(newchecks);
return tokerr; return tokerr;
}
tok = tok->link(); tok = tok->link();
if (!tok) if (!tok)
{
dealloc_checks(newchecks);
return 0; return 0;
}
} }
std::list<ExecutionPath *>::iterator it; std::list<ExecutionPath *>::iterator it;
for (it = newchecks.begin(); it != newchecks.end(); ++it) for (it = newchecks.begin(); it != newchecks.end(); ++it)
checks.push_back((*it)->copy()); checks.push_back(*it);
} }
@ -193,7 +213,6 @@ static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPa
return 0; return 0;
} }
void checkExecutionPaths(const Token *tok, ExecutionPath *c) void checkExecutionPaths(const Token *tok, ExecutionPath *c)
{ {
for (; tok; tok = tok->next()) for (; tok; tok = tok->next())

View File

@ -1693,7 +1693,13 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
if (macro->name().empty()) if (macro->name().empty())
delete macro; delete macro;
else else
{
std::map<std::string, PreprocessorMacro *>::iterator it;
it = macros.find(macro->name());
if (it != macros.end())
delete it->second;
macros[macro->name()] = macro; macros[macro->name()] = macro;
}
line = "\n"; line = "\n";
} }
@ -1759,6 +1765,11 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
errorLogger, errorLogger,
"noQuoteCharPair", "noQuoteCharPair",
std::string("No pair for character (") + ch + "). Can't process file. File is either invalid or unicode, which is currently not supported."); std::string("No pair for character (") + ch + "). Can't process file. File is either invalid or unicode, which is currently not supported.");
std::map<std::string, PreprocessorMacro *>::iterator it;
for (it = macros.begin(); it != macros.end(); ++it)
delete it->second;
return ""; return "";
} }
@ -1882,11 +1893,10 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
errorLogger, errorLogger,
"syntaxError", "syntaxError",
std::string("Syntax error. Not enough parameters for macro '") + macro->name() + "'."); std::string("Syntax error. Not enough parameters for macro '") + macro->name() + "'.");
{
std::map<std::string, PreprocessorMacro *>::iterator it; std::map<std::string, PreprocessorMacro *>::iterator it;
for (it = macros.begin(); it != macros.end(); ++it) for (it = macros.begin(); it != macros.end(); ++it)
delete it->second; delete it->second;
}
return ""; return "";
} }