From 9d11492845f47cf2555f2e2e390babedf575dbfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 27 Jan 2010 22:05:04 +0100 Subject: [PATCH] Fixed #1286 (Memory leak not detected) --- lib/checkmemoryleak.cpp | 20 ++++++++------------ test/testmemleak.cpp | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index f83785164..58ce15527 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2257,10 +2257,9 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vectorstr() != "{") tok1 = tok1->next(); - if (tok1) - tok1 = tok1->next(); + tok1 = tok1 ? tok1->next() : 0; - int indentlevel = 0; + unsigned int indentlevel = 0; for (const Token *tok = tok1; tok; tok = tok->next()) { if (tok->str() == "{") @@ -2268,9 +2267,9 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vectorstr() == "}") { - --indentlevel; - if (indentlevel < 0) + if (indentlevel == 0) return; + --indentlevel; } // Only parse this particular class.. not subclasses @@ -2292,17 +2291,14 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vectornext(), "%type% * %var% ;")) + if (Token::Match(tok->previous(), ";|{|}|private:|protected:|public: %type% * %var% ;")) { // No false positives for auto deallocated classes.. - if (_settings->isAutoDealloc(tok->strAt(1))) + if (_settings->isAutoDealloc(tok->str().c_str())) continue; - if (tok->isName() || Token::Match(tok, "[;}]")) - { - if (_settings->_showAll || !isclass(_tokenizer, tok->tokAt(1))) - variable(classname.back(), tok->tokAt(3)); - } + if (_settings->_showAll || !isclass(_tokenizer, tok)) + variable(classname.back(), tok->tokAt(2)); } } } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 5d0f5d4ff..58634963b 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2721,6 +2721,7 @@ private: TEST_CASE(class11); TEST_CASE(class12); TEST_CASE(class13); + TEST_CASE(class14); TEST_CASE(staticvar); @@ -2989,6 +2990,20 @@ private: ASSERT_EQUALS("", errout.str()); } + void class14() + { + check("class A\n" + "{\n" + " int *p;\n" + "public:\n" + " void init();\n" + "};\n" + "\n" + "void A::init()\n" + "{ p = new int[10]; }\n", true); + ASSERT_EQUALS("[test.cpp:3]: (possible error) Memory leak: A::p\n", errout.str()); + } + void staticvar() {