diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index d417407e7..3450bdb2a 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -155,6 +155,10 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] { Alloc = alloc; alloc_indentlevel = indentlevel; + + // Is there a comment such as "var is deleted automaticly" + if ( Alloc == New && isdeleted(tok->str) > 0 ) + return; } } diff --git a/tests.cpp b/tests.cpp index 8bef178cb..b55f6c689 100644 --- a/tests.cpp +++ b/tests.cpp @@ -585,6 +585,16 @@ static void memleak_in_function() + + const char test13[] = "static char *f()\n" + "{\n" + " Fred *fred = new Fred;\n" + " // fred is deleted automaticly\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, test13, "" ); + + + } //--------------------------------------------------------------------------- diff --git a/tokenize.cpp b/tokenize.cpp index c422e61ea..08543b947 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -287,8 +287,18 @@ void Tokenize(const char FileName[]) // Tokenize - tokenizes input stream //--------------------------------------------------------------------------- +struct DeleteComment +{ + unsigned int LineNr; + std::string Str; +}; + +std::list< DeleteComment > DeleteComments; + void TokenizeCode(std::istream &code, const unsigned int FileIndex) { + DeleteComments.clear(); + // Tokenize the file. unsigned int lineno = 1; char CurrentToken[1000] = {0}; @@ -391,6 +401,8 @@ void TokenizeCode(std::istream &code, const unsigned int FileIndex) // Comments.. if (ch == '/' && !code.eof()) { + bool newstatement = bool( strchr(";{}", CurrentToken[0]) != NULL ); + // Add current token.. addtoken(CurrentToken, lineno, FileIndex); memset(CurrentToken, 0, sizeof(CurrentToken)); @@ -402,7 +414,15 @@ void TokenizeCode(std::istream &code, const unsigned int FileIndex) // If '//'.. if (ch == '/') { - while (!code.eof() && (char)code.get()!='\n'); + std::string comment; + getline( code, comment ); + if ( newstatement && comment.find(" delete")!=std::string::npos ) + { + DeleteComment dc; + dc.LineNr = lineno; + dc.Str = comment; + DeleteComments.push_back( dc ); + } lineno++; continue; } @@ -1093,4 +1113,15 @@ void DeallocateTokens() +int isdeleted( const char varname[] ) +{ + std::list::const_iterator it; + for ( it = DeleteComments.begin(); it != DeleteComments.end(); it++ ) + { + const DeleteComment &dc = *it; + if ( dc.Str.find( varname ) != std::string::npos ) + return dc.LineNr; + } + return -1; +} diff --git a/tokenize.h b/tokenize.h index 3add04311..d8bc250fb 100644 --- a/tokenize.h +++ b/tokenize.h @@ -39,6 +39,11 @@ const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]); const TOKEN *gettok(const TOKEN *tok, int index); const char *getstr(const TOKEN *tok, int index); + +// Delete comments such as "fred is deleted automaticly" +int isdeleted( const char varname[] ); + + //--------------------------------------------------------------------------- #endif