diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 625d2b333..ce58a7ba5 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -124,8 +124,9 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] AllocType Alloc = No; - int alloc_indentlevel = 0; - int dealloc_indentlevel = 0; + int alloc_indentlevel = -1; + int dealloc_indentlevel = -1; + std::list loop_indentlevel; bool isif = false; @@ -145,6 +146,9 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] return; } + if ( !loop_indentlevel.empty() && indentlevel <= loop_indentlevel.back() ) + loop_indentlevel.pop_back(); + if ( indentlevel < alloc_indentlevel ) alloc_indentlevel = -1; @@ -152,6 +156,10 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] dealloc_indentlevel = -1; } + // for, while set loop level.. + if ( alloc_indentlevel >= 0 && (Match(tok,"while") || Match(tok,"for")) ) + loop_indentlevel.push_back( indentlevel ); + // Skip stuff like: if (!var) ... if ( Match(tok, "if ( ! %var1% )", varnames) || Match(tok, "if ( unlikely ( ! %var1% ) )", varnames) || @@ -269,7 +277,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] // continue/break loop.. if (Alloc != No && - alloc_indentlevel == indentlevel && + loop_indentlevel.empty() && (Match(tok,"continue") || Match(tok,"break"))) { MemoryLeak( tok, varname );