diff --git a/main.cpp b/main.cpp index dfec60cc2..14d8a439a 100644 --- a/main.cpp +++ b/main.cpp @@ -1150,10 +1150,16 @@ void CheckMemoryLeak() { std::vector<_variable *> varlist; + // if int iflevel = 0; bool endif = false; std::vector iflist; + // loop + int looplevel = 0; + bool endloop = false; + std::vector looplist; + // Parse the statement list and locate memory leaks.. int indentlevel = 0; std::list::const_iterator it; @@ -1166,6 +1172,9 @@ void CheckMemoryLeak() iflist.push_back(endif); if (endif) iflevel++; + looplist.push_back(endloop); + if (endloop) + looplevel++; break; case STATEMENT::EBRACE: @@ -1195,6 +1204,11 @@ void CheckMemoryLeak() iflevel--; iflist.pop_back(); + // loop level.. + if (looplist.back()) + looplevel--; + looplist.pop_back(); + // Make sure the varlist is empty.. if (indentlevel <= 1) varlist.clear(); @@ -1316,9 +1330,45 @@ void CheckMemoryLeak() } } break; + + case STATEMENT::CONTINUE: + case STATEMENT::BREAK: + // Memory leak if variable is allocated.. + if (looplevel>0) + { + // Find the last loop.. + for (int i = looplist.size() - 1; i >= 0; i--) + { + if (!looplist[i]) + continue; + + int loop_indentlevel = i + 1; + + for (i = 0; i < (int)varlist.size(); i++) + { + if (varlist[i]->indentlevel < loop_indentlevel) + continue; + + if (varlist[i]->value==_variable::Malloc || + varlist[i]->value==_variable::New || + varlist[i]->value==_variable::NewA ) + { + std::ostringstream ostr; + ostr << FileLine(it->Token) << ": Memory leak:" << VariableNames[varlist[i]->varindex]; + ReportErr(ostr.str()); + } + + break; + } + + break; + } + } + break; } endif = (it->Type == STATEMENT::ENDIF); + endloop = (it->Type == STATEMENT::ENDLOOP); } }