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() {