Boost logo

Boost Users :

From: Pierre-Jules Tremblay (iamtrep_at_[hidden])
Date: 2006-03-29 09:18:51


We use Boost.Test extensively as our unit test framework. Recently,
we introduced some code to check for memory leaks. Each test case is
expected to release all the memory it allocates (i.e. fixtures and
what not). However, we observe that several of our test cases appear
to leak memory when in fact they don't. For instance, a memory leak
check when main() exits reveals no leaks, even though leaks were
"found" in several test cases. Our observation is that the apparent
leaks appear and disappear as we modify the test code.

Scrutinizing the code has revealed that adding a BOOST_REQUIRE(true)
here and there in the problematic test cases can make this problem go
away. Is it at all possible that Boost.Test somehow allocates memory
during a test case run, say for logging or other purpose, and doesn't
release it before the test case ends?

Here is the code we use to check for memory leaks after each test case is run:

#define DECLARE_TEST_CASE(name) \
void test##name##__(); \
void test##name##() \
{ \
   mytestlib::TestCaseResetLeakCheck(); \
   const uint32 uiMemUsageBeforeTest = MMGetAllocatedBytes(); \
   test##name##__(); \
   const uint32 uiMemUsageAfterTest = MMGetAllocatedBytes(); \
   const uint32 uiMemUsageDiff = uiMemUsageBeforeTest >
uiMemUsageAfterTest ? uiMemUsageBeforeTest - uiMemUsageAfterTest :
uiMemUsageAfterTest - uiMemUsageBeforeTest; \
   if (uiMemUsageDiff) \
   { \
       if (mytestlib::TestCaseLeakCheck()) \
       { \
           BOOST_ERROR("Test " #name " has memory leaks (" <<
uiMemUsageAfterTest << " != " << uiMemUsageBeforeTest << ", " <<
uiMemUsageDiff << " bytes leaked)"); \
       } \
       else \
       { \
           BOOST_WARN_MESSAGE(false,"Test " #name " has memory leaks
(" << uiMemUsageAfterTest << " != " << uiMemUsageBeforeTest << ", " <<
uiMemUsageDiff << " bytes leaked) (marked as expected)"); \
       } \
   } \
} \
void test##name##__()

DECLARE_TEST_CASE( SomeTest )
{
  // arbitrary test code goes here...
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net