Fixed #1286 (Memory leak not detected)

This commit is contained in:
Daniel Marjamäki 2010-01-27 22:05:04 +01:00
parent 15b4abd6d3
commit 9d11492845
2 changed files with 23 additions and 12 deletions

View File

@ -2257,10 +2257,9 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vector<std::stri
// Go into class. // Go into class.
while (tok1 && tok1->str() != "{") while (tok1 && tok1->str() != "{")
tok1 = tok1->next(); tok1 = tok1->next();
if (tok1) tok1 = tok1 ? tok1->next() : 0;
tok1 = tok1->next();
int indentlevel = 0; unsigned int indentlevel = 0;
for (const Token *tok = tok1; tok; tok = tok->next()) for (const Token *tok = tok1; tok; tok = tok->next())
{ {
if (tok->str() == "{") if (tok->str() == "{")
@ -2268,9 +2267,9 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vector<std::stri
else if (tok->str() == "}") else if (tok->str() == "}")
{ {
--indentlevel; if (indentlevel == 0)
if (indentlevel < 0)
return; return;
--indentlevel;
} }
// Only parse this particular class.. not subclasses // Only parse this particular class.. not subclasses
@ -2292,17 +2291,14 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vector<std::stri
} }
// Declaring member variable.. check allocations and deallocations // Declaring member variable.. check allocations and deallocations
if (Token::Match(tok->next(), "%type% * %var% ;")) if (Token::Match(tok->previous(), ";|{|}|private:|protected:|public: %type% * %var% ;"))
{ {
// No false positives for auto deallocated classes.. // No false positives for auto deallocated classes..
if (_settings->isAutoDealloc(tok->strAt(1))) if (_settings->isAutoDealloc(tok->str().c_str()))
continue; continue;
if (tok->isName() || Token::Match(tok, "[;}]")) if (_settings->_showAll || !isclass(_tokenizer, tok))
{ variable(classname.back(), tok->tokAt(2));
if (_settings->_showAll || !isclass(_tokenizer, tok->tokAt(1)))
variable(classname.back(), tok->tokAt(3));
}
} }
} }
} }

View File

@ -2721,6 +2721,7 @@ private:
TEST_CASE(class11); TEST_CASE(class11);
TEST_CASE(class12); TEST_CASE(class12);
TEST_CASE(class13); TEST_CASE(class13);
TEST_CASE(class14);
TEST_CASE(staticvar); TEST_CASE(staticvar);
@ -2989,6 +2990,20 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void class14()
{
check("class A\n"
"{\n"
" int *p;\n"
"public:\n"
" void init();\n"
"};\n"
"\n"
"void A::init()\n"
"{ p = new int[10]; }\n", true);
ASSERT_EQUALS("[test.cpp:3]: (possible error) Memory leak: A::p\n", errout.str());
}
void staticvar() void staticvar()
{ {