Created a new class for detecting leaks (forgetting to free struct members)

This commit is contained in:
Daniel Marjamäki 2009-06-16 22:01:04 +02:00
parent ed98f1b367
commit b7c9a4ed8c
2 changed files with 102 additions and 0 deletions

View File

@ -223,5 +223,58 @@ private:
}; };
/**
* Check class variables
* variables that are allocated in the constructor should be deallocated in the destructor
*/
class CheckMemoryLeakStructMember : public CheckMemoryLeak, public Check
{
public:
CheckMemoryLeakStructMember() : Check()
{ }
CheckMemoryLeakStructMember(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(tokenizer, settings, errorLogger)
{ }
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
CheckMemoryLeakStructMember checkMemoryLeak(tokenizer, settings, errorLogger);
checkMemoryLeak.check();
}
void check()
{
// TODO
}
private:
void error(const Token *tok, const std::string &severity, const std::string &id, const std::string &msg)
{
reportError(tok, severity, id, msg);
}
void error(const std::list<const Token *> &callstack, const std::string &severity, const std::string &id, const std::string &msg)
{
reportError(callstack, severity, id, msg);
}
void getErrorMessages()
{ }
std::string name() const
{
return "Memory leaks (struct members)";
}
std::string classInfo() const
{
return "Don't forget to free struct members";
}
};
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif #endif

View File

@ -2549,3 +2549,52 @@ private:
static TestMemleakInClass testMemleakInClass; static TestMemleakInClass testMemleakInClass;
class TestMemleakStructMember : public TestFixture
{
public:
TestMemleakStructMember() : TestFixture("TestMemleakStructMember")
{ }
private:
void check(const char code[])
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
// Clear the error buffer..
errout.str("");
// Check for memory leaks..
Settings settings;
CheckMemoryLeakStructMember checkMemoryLeakStructMember(&tokenizer, &settings, this);
checkMemoryLeakStructMember.check();
}
void run()
{
TEST_CASE(test1);
}
void test1()
{
check("static void foo()\n"
"{\n"
" struct ABC abc;\n"
" abc.a = malloc(10);\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: abc.a\n", errout.str());
}
};
static TestMemleakStructMember testMemleakStructMember;