cpputest: static variable causes CppUtest to cry about a false memory leak.

result hi

I am using Visual Studio 2013 and std::regex c++11 with CppUtest 3.8 release. I have a regex expression declared as a static variable in a function of a library class that is used in tests. The fact that it is static makes it lie to me about memory leaks and fail the tests. It does so with the regex variable but not with something more simple like std::string or int.

Library Project

MyClass.h

class MyClass
{
public:
    static bool matchesRegex(char *s);
};

MyClass.cpp

#include "MyClass.h"

#include <regex>

bool MyClass::matchesRegex(char *s)
{
    static std::regex IrExp("wazaaaa");

    return std::regex_match(s, IrExp);
}

Unit tests Project

main.cpp

#include <CppUTest/CommandLineTestRunner.h>

#ifndef DEBUG
#pragma comment(lib, "CppUTest.lib")
#else
#pragma comment(lib, "CppUTestd.lib")
#endif

int main(int argc, char** argv)
{
    int result = CommandLineTestRunner::RunAllTests(argc, argv);
    system("pause");
    return result;
}

TestTest.cpp

#include "CppUTest/TestHarness.h"
#include "../Project1/MyClass.h"

TEST_GROUP(MyClassTest)
{

};

TEST(MyClassTest, DA_TEST)
{
    bool result = MyClass::matchesRegex("wazaaaa");

    CHECK(result);
}

I linked stuff through project properties to link the library projetc with the test one. that is all.

image

source files.zip

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 22 (13 by maintainers)

Most upvoted comments

CppUTest is not lying to you. It is telling you that during the run of your test, your code allocated memory and did not release it before the test ended. As far as CppUTest is concerned that is a leak. It did its job nicely.

There are several design changes that can fix it. Is this library something you own and maintain? If so, I suggest a design that does not look like a leak.

If it is not code that you control, then before your main calls RunAllTests, you could call matchesRegex, so the allocation happens before CppUTest starts paying attention to your code’s allocations.

Hope that helps, James


James Grenning - Author of TDD for Embedded C - wingman-sw.com/tddec wingman-sw.com wingman-sw.com/blog twitter.com/jwgrenning facebook.com/wingman.sw wingman software On 18 Sep 2016, at 14:04, Infogeekx wrote:

hi

I am using Visual Studio 2013 and std::regex c++11 with CppUtest 3.8 release. I have a regex expression declared as a static variable in a function of a library class that is used in tests. The fact that it is static makes it lie to me about memory leaks and fail the tests. It does so with the regex variable but not with something more simple like std::string or int.

Library Project

MyClass.h

class MyClass
{
public:
  static bool matchesRegex(char *s);
};

MyClass.cpp

#include "MyClass.h"

#include <regex>

bool MyClass::matchesRegex(char *s)
{
  static std::regex IrExp("wazaaaa");

  return std::regex_match(s, IrExp);
}

Unit tests Project

main.cpp

#include <CppUTest/CommandLineTestRunner.h>

#ifndef DEBUG
#pragma comment(lib, "CppUTest.lib")
#else
#pragma comment(lib, "CppUTestd.lib")
#endif

int main(int argc, char** argv)
{
  int result = CommandLineTestRunner::RunAllTests(argc, argv);
  system("pause");
  return result;
}

TestTest.cpp

#include "CppUTest/TestHarness.h"
#include "../Project1/MyClass.h"

TEST_GROUP(MyClassTest)
{

};

TEST(MyClassTest, DA_TEST)
{
  bool result = MyClass::matchesRegex("wazaaaa");

  CHECK(result);
}

I linked stuff through project properties to link the library projetc with the test one. that is all.

image

You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/cpputest/cpputest/issues/1043