From ca56520c298bf92ef84db80f9f0586a4cb2aee70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 23 Jan 2009 19:24:52 +0000 Subject: [PATCH] memory leak: don't give false positive when deallocating member variable this->i (#53) --- src/checkmemoryleak.cpp | 11 +++++++++-- test/testmemleak.cpp | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index b0db91f6f..32380da2a 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -1406,7 +1406,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const std::vect { destructor << classname[i] << " :: "; } - destructor << " ~ " << classname.back() << " ("; + destructor << "~ " << classname.back() << " ("; // Pattern used in member function. "Var = ..." std::ostringstream varname_eq; @@ -1461,9 +1461,15 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const std::vect } // Deallocate.. - const char *varnames[2] = { "var", 0 }; + const char *varnames[3] = { "var", 0, 0 }; varnames[0] = varname; AllocType dealloc = GetDeallocationType(tok, varnames); + if (dealloc == No) + { + varnames[0] = "this"; + varnames[1] = varname; + dealloc = GetDeallocationType(tok, varnames); + } if (dealloc != No) { std::list callstack; @@ -1473,6 +1479,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const std::vect MismatchError(tok, callstack, FullVariableName.str().c_str()); Dealloc = dealloc; } + } } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index f49a31ded..48b185950 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -139,6 +139,7 @@ private: TEST_CASE(class4); TEST_CASE(class5); TEST_CASE(class6); + TEST_CASE(class7); TEST_CASE(throw1); TEST_CASE(throw2); @@ -1312,6 +1313,28 @@ private: ASSERT_EQUALS("", errout.str()); } + void class7() + { + check("class Fred\n" + "{\n" + "public:\n" + " int *i;\n" + " Fred();\n" + " ~Fred();\n" + "};\n" + "\n" + "Fred::Fred()\n" + "{\n" + " this->i = new int;\n" + "}\n" + "Fred::~Fred()\n" + "{\n" + " delete this->i;\n" + "}\n", true); + ASSERT_EQUALS("", errout.str()); + } + +