From 701d622ff09194c672e54615bf64c70fd562b008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 22 Oct 2009 21:51:58 +0200 Subject: [PATCH] Fixed #428 (Memory leak not detected with class) --- src/checkmemoryleak.cpp | 10 +++++++++- test/testmemleak.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index ff9fb7e76..495328ede 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -2100,6 +2100,8 @@ void CheckMemoryLeakInClass::variable(const char classname[], const Token *tokVa const Token *functionToken = Tokenizer::findClassFunction(_tokenizer->tokens(), classname, "~| %var%", indent_); while (functionToken) { + const bool destructor(functionToken->tokAt(2)->str() == "~"); + int indent = 0; bool initlist = false; 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) continue; + if (!destructor) + continue; + + // Function call.. + if (Token::Match(tok, "[{};] %var% (")) + return; + // Deallocate.. const char *varnames[3] = { "var", 0, 0 }; varnames[0] = varname; @@ -2171,7 +2180,6 @@ void CheckMemoryLeakInClass::variable(const char classname[], const Token *tokVa Dealloc = dealloc; } - } } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 2b45d9f73..235d410f2 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2467,6 +2467,7 @@ private: TEST_CASE(class9); TEST_CASE(class10); TEST_CASE(class11); + TEST_CASE(class12); TEST_CASE(staticvar); @@ -2687,6 +2688,29 @@ private: 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() {