the Compartmented Robust Posix C++ Unit Test system |
|
---|
Assert that all heap objects allocated in the the block of code immediately following the macro, are also deallocated in the same block.
Used in: A test function body, the constructor or destructor of a
fixture, or a function called from them.
See TEST(name, ...)
.
Requirement: A code block follows immediately after.
Requirement: The test program must be linked with -lcrpcut
instead
of -lcrpcut_basic
.
The assertion succeeds if, and only if, all heap objects allocated in the code block following are also deallocated in the block, so that the objects on the heap are exactly the same before the code of block begins and after the code of block ends.
On success the test function continues without side effects.
On failure, the test is terminated with an error report. The report includes all heap objects allocated in the block of code that still exists after the destructors for the block of code have finished.
Tip | |
---|---|
Use the command line flag
-b /
--backtrace-heap to display the
full stack backtrace for allocation of each object. |
See also:
VERIFY_SCOPE_HEAP_LEAK_FREE
.
Example: The test program
#include <crpcut.hpp> #include <string> TEST(heap_in_balance) { std::string before("a"); ASSERT_SCOPE_HEAP_LEAK_FREE { std::string after("b"); } } TEST(heap_imbalance) { std::string before("a"); ASSERT_SCOPE_HEAP_LEAK_FREE { // This is not a memory leak, but it is reported std::string after("b"); // as such because the object allocated here is std::swap(before, after); // not released at the end of the block } } TEST(heap_leak) { char *p[5]; ASSERT_SCOPE_HEAP_LEAK_FREE { int i; for (i = 0; i < 5; ++i) { p[i] = new char[i+1]; } while (--i > 0) { delete[] p[i]; } } } int main(int argc, char *argv[]) { return crpcut::run(argc, argv); }
reports two failed test:
FAILED!: heap_imbalance phase="running" -------------------------------------------------------------- samples/assert_scope_leak_free.cpp:43 ASSERT_SCOPE_LEAK_FREE 1 object 26 bytes at 0x62ba20 allocated with new Allocated at: /var/tmp/build/lib64/libcrpcut.so.1(operator new(unsigned long)+0x1d) [0x7fca98f49984] /usr/lib/gcc/x86_64-pc-linux-gnu/5.3.0/libstdc++.so.6(std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&)+0x59) [0x384b4d8f29] ------------------------------------------------------------------------------- =============================================================================== FAILED!: heap_leak phase="running" -------------------------------------------------------------- samples/assert_scope_leak_free.cpp:53 ASSERT_SCOPE_LEAK_FREE 1 object 1 byte at 0x62b860 allocated with new[] Allocated at: /var/tmp/build/lib64/libcrpcut.so.1(operator new[](unsigned long)+0x1d) [0x7fca98f499e9] ../test/assert_scope_leak_free(heap_leak::test()+0x8f) [0x4088cf] ------------------------------------------------------------------------------- =============================================================================== 3 test cases selected Sum Critical Non-critical PASSED : 1 1 0 FAILED : 2 2 0