Fixed #428 (Memory leak not detected with class)

This commit is contained in:
Daniel Marjamäki 2009-10-22 21:51:58 +02:00
parent b80a954f55
commit 701d622ff0
2 changed files with 33 additions and 1 deletions

View File

@ -2100,6 +2100,8 @@ void CheckMemoryLeakInClass::variable(const char classname[], const Token *tokVa
const Token *functionToken = Tokenizer::findClassFunction(_tokenizer->tokens(), classname, "~| %var%", indent_); const Token *functionToken = Tokenizer::findClassFunction(_tokenizer->tokens(), classname, "~| %var%", indent_);
while (functionToken) while (functionToken)
{ {
const bool destructor(functionToken->tokAt(2)->str() == "~");
int indent = 0; int indent = 0;
bool initlist = false; bool initlist = false;
for (const Token *tok = functionToken; tok; tok = tok->next()) for (const Token *tok = functionToken; tok; tok = tok->next())
@ -2146,6 +2148,13 @@ void CheckMemoryLeakInClass::variable(const char classname[], const Token *tokVa
if (indent == 0) if (indent == 0)
continue; continue;
if (!destructor)
continue;
// Function call..
if (Token::Match(tok, "[{};] %var% ("))
return;
// Deallocate.. // Deallocate..
const char *varnames[3] = { "var", 0, 0 }; const char *varnames[3] = { "var", 0, 0 };
varnames[0] = varname; varnames[0] = varname;
@ -2171,7 +2180,6 @@ void CheckMemoryLeakInClass::variable(const char classname[], const Token *tokVa
Dealloc = dealloc; Dealloc = dealloc;
} }
} }
} }

View File

@ -2467,6 +2467,7 @@ private:
TEST_CASE(class9); TEST_CASE(class9);
TEST_CASE(class10); TEST_CASE(class10);
TEST_CASE(class11); TEST_CASE(class11);
TEST_CASE(class12);
TEST_CASE(staticvar); TEST_CASE(staticvar);
@ -2687,6 +2688,29 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (possible error) Memory leak: A::p\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Memory leak: A::p\n", errout.str());
} }
void class12()
{
check("class A\n"
"{\n"
"private:\n"
" int *p;\n"
"public:\n"
" A();\n"
" ~A();\n"
" void cleanup();"
"};\n"
"\n"
"A::A()\n"
"{ p = new int[10]; }\n"
"\n"
"A::~A()\n"
"{ }\n"
"\n"
"void A::cleanup()\n"
"{ delete [] p; }\n", true);
ASSERT_EQUALS("[test.cpp:4]: (possible error) Memory leak: A::p\n", errout.str());
}
void staticvar() void staticvar()
{ {