From 54c5d53eac4146e5505d6bdf3a13bce8b7fbafa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 19 Mar 2010 16:57:23 +0100 Subject: [PATCH] Fixed #1428 (False positive: (possible error) Memory leak when memory deleted in destructor) --- lib/checkmemoryleak.cpp | 4 ++-- test/testmemleak.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index f5ccd93e3..aa31cb349 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2338,8 +2338,8 @@ void CheckMemoryLeakInClass::variable(const std::string &classname, const Token const Token *functionToken = _tokenizer->findClassFunction(_tokenizer->tokens(), classname.c_str(), "~| %var%", indent_); while (functionToken) { - const bool constructor(Token::Match(functionToken, (classname + " :: " + classname + " (").c_str())); - const bool destructor(functionToken->tokAt(2)->str() == "~"); + const bool constructor(Token::Match(functionToken, (classname + " (").c_str()) || Token::Match(functionToken, (classname + " :: " + classname + " (").c_str())); + const bool destructor(functionToken->str() == "~" || functionToken->tokAt(2)->str() == "~"); unsigned int indent = 0; bool initlist = false; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index dd8035507..8fbb303fc 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2738,6 +2738,7 @@ private: TEST_CASE(class12); TEST_CASE(class13); TEST_CASE(class14); + TEST_CASE(class15); TEST_CASE(staticvar); @@ -3020,6 +3021,20 @@ private: ASSERT_EQUALS("[test.cpp:3]: (possible error) Memory leak: A::p\n", errout.str()); } + void class15() + { + check("class A\n" + "{\n" + " int *p;\n" + "public:\n" + " A();\n" + " ~A() { delete [] p; }\n" + "};\n" + "A::A()\n" + "{ p = new int[10]; }", true); + ASSERT_EQUALS("", errout.str()); + } + void staticvar() {