Boost.Test - memory allocated during test case run prevents memory leak detection?
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... }
Sorry, for delay response. I've been extremely busy lately.
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?
I found similar issues when designed exception safety testing facilities. I tried to minimize an amount of dynamically allocated memory with Boost.Test code. Also I am using guard that tell me when I enter/leave Boost.Test code and stop/start recording memory allocations. It's still not 100% bulletproof. It's possible that some static/global variables access from within test case may cause memory allocation. You will need to deal with it in case by case level. Try to wrap Boost.Test tools with your guards. Gennadiy
Thanks for your reply.
It turns out that it was our static data (cough) doing those
allocations. But wrapping the BOOST_ tools is a good idea - I think
I'll implement it anyway.
Cheers,
Pierre
On 4/12/06, Gennadiy Rozental
Sorry, for delay response. I've been extremely busy lately.
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?
I found similar issues when designed exception safety testing facilities. I tried to minimize an amount of dynamically allocated memory with Boost.Test code. Also I am using guard that tell me when I enter/leave Boost.Test code and stop/start recording memory allocations. It's still not 100% bulletproof. It's possible that some static/global variables access from within test case may cause memory allocation. You will need to deal with it in case by case level. Try to wrap Boost.Test tools with your guards.
Gennadiy
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Gennadiy Rozental
-
Pierre-Jules Tremblay