Memory leaks: Determine that some classes are not auto-deallocated. Reduce false negatives. Ticket: #2219 and #1618

This commit is contained in:
Robert Reif 2010-12-04 07:29:12 +01:00 committed by Daniel Marjamäki
parent ca8f5015b8
commit 758fc85a12
3 changed files with 55 additions and 3 deletions

View File

@ -2616,9 +2616,18 @@ void CheckMemoryLeakInClass::check()
variable(info, var->token); variable(info, var->token);
} }
else
// known class?
else if (var->type)
{ {
/** @todo false negative: check classes here someday */ // not derived and no constructor?
if (var->type->derivedFrom.empty() && var->type->numConstructors == 0)
{
if (var->access == SymbolDatabase::Private)
checkPublicFunctions(info, var->token);
variable(info, var->token);
}
} }
} }
} }

View File

@ -1087,21 +1087,25 @@ void SymbolDatabase::SpaceInfo::getVarList()
else if (Token::Match(tok, "%type% * %var% ;")) else if (Token::Match(tok, "%type% * %var% ;"))
{ {
vartok = tok->tokAt(2); vartok = tok->tokAt(2);
typetok = tok;
tok = vartok->next(); tok = vartok->next();
} }
else if (Token::Match(tok, "%type% %type% * %var% ;")) else if (Token::Match(tok, "%type% %type% * %var% ;"))
{ {
vartok = tok->tokAt(3); vartok = tok->tokAt(3);
typetok = vartok->tokAt(-2);
tok = vartok->next(); tok = vartok->next();
} }
else if (Token::Match(tok, "%type% :: %type% * %var% ;")) else if (Token::Match(tok, "%type% :: %type% * %var% ;"))
{ {
vartok = tok->tokAt(4); vartok = tok->tokAt(4);
typetok = vartok->tokAt(-2);
tok = vartok->next(); tok = vartok->next();
} }
else if (Token::Match(tok, "%type% :: %type% :: %type% * %var% ;")) else if (Token::Match(tok, "%type% :: %type% :: %type% * %var% ;"))
{ {
vartok = tok->tokAt(6); vartok = tok->tokAt(6);
typetok = vartok->tokAt(-2);
tok = vartok->next(); tok = vartok->next();
} }
@ -1221,7 +1225,7 @@ void SymbolDatabase::SpaceInfo::getVarList()
const SpaceInfo *spaceInfo = NULL; const SpaceInfo *spaceInfo = NULL;
if (isClass) if (typetok)
spaceInfo = check->findVarType(this, typetok); spaceInfo = check->findVarType(this, typetok);
addVar(vartok, varaccess, isMutable, isStatic, isConst, isClass, spaceInfo); addVar(vartok, varaccess, isMutable, isStatic, isConst, isClass, spaceInfo);

View File

@ -3579,6 +3579,45 @@ private:
" rp2 = new TRadioButton(this);\n" " rp2 = new TRadioButton(this);\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("class TRadioButton { };\n"
"class Foo\n"
"{\n"
"private:\n"
" TRadioButton* rp1;\n"
" TRadioButton* rp2;\n"
"public:\n"
" Foo();\n"
"};\n"
"Foo::Foo()\n"
"{\n"
" rp1 = new TRadioButton;\n"
" rp2 = new TRadioButton;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: Foo::rp1\n"
"[test.cpp:6]: (error) Memory leak: Foo::rp2\n", errout.str());
check("class TRadioButton { };\n"
"class Foo\n"
"{\n"
"private:\n"
" TRadioButton* rp1;\n"
" TRadioButton* rp2;\n"
"public:\n"
" Foo();\n"
" ~Foo();\n"
"};\n"
"Foo::Foo()\n"
"{\n"
" rp1 = new TRadioButton;\n"
" rp2 = new TRadioButton;\n"
"}\n"
"Foo::~Foo()\n"
"{\n"
" delete rp1;\n"
" delete rp2;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void staticvar() void staticvar()