diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index ec0a12cf1..fcbf18138 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -385,7 +385,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] } // Return without deallocating the memory.. - if ( Alloc != No && alloc_indentlevel >= 0 && dealloc_indentlevel <= 0 && Match(tok, "return") ) + if ( Alloc != No && (indentlevel==0 || (alloc_indentlevel >= 0 && dealloc_indentlevel <= 0)) && Match(tok, "return") ) { bool retvar = false; for ( const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next ) @@ -435,8 +435,15 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] } } - if ( indentlevel <= alloc_indentlevel ) + if ( indentlevel == 0 ) return; + + if ( indentlevel <= alloc_indentlevel ) + { + Alloc = No; + alloc_indentlevel = -1; + dealloc_indentlevel = -1; + } } } } diff --git a/tests.cpp b/tests.cpp index 32858211d..ea802f033 100644 --- a/tests.cpp +++ b/tests.cpp @@ -595,6 +595,18 @@ static void memleak_in_function() check( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Memory leak: s\n" ); + code = "static char *f()\n" + "{\n" + " char *s;\n" + " if ( abc )\n" + " {\n" + " s = new char[10];\n" + " }\n" + " return s;\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, code, "" ); + +