the Compartmented Robust Posix C++ Unit Test system

VERIFY_SCOPE_HEAP_LEAK_FREE

Verify 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 check 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 marked as failed, but continues execution 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]Tip
Use the command line flag -b / --backtrace-heap to display the full stack backtrace for allocation of each object.

See also: ASSERT_SCOPE_HEAP_LEAK_FREE.

Example: The test program

     
     #include <crpcut.hpp>
     #include <string>
     
     TEST(heap_in_balance)
     {
       std::string before("a");
       VERIFY_SCOPE_HEAP_LEAK_FREE
       {
         std::string after("b");
       }
     }
     
     TEST(heap_imbalance)
     {
       VERIFY_SCOPE_HEAP_LEAK_FREE     // no leak reported here
       {
         std::string before("a");
         VERIFY_SCOPE_HEAP_LEAK_FREE
         {                             // This is not a memory leak, but it is
           std::string after("b");     // reported as such because the object
           std::swap(before, after);   // allocated here is not released at the
         }                             // end of the block
       }
     }
     
     TEST(heap_leak)
     {
       char *p[5];
       VERIFY_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
     fail---------------------------------------------------------------------------
     samples/verify_scope_leak_free.cpp:45
     VERIFY_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) [0x7f88e44aa984]
     /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]
     -------------------------------------------------------------------------------
     phase="running"  --------------------------------------------------------------
     samples/verify_scope_leak_free.cpp:40
     Earlier VERIFY failed
     -------------------------------------------------------------------------------
     ===============================================================================
     FAILED!: heap_leak
     fail---------------------------------------------------------------------------
     samples/verify_scope_leak_free.cpp:56
     VERIFY_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) [0x7f88e44aa9e9]
     ../test/verify_scope_leak_free(heap_leak::test()+0x8f) [0x40894d]
     -------------------------------------------------------------------------------
     phase="running"  --------------------------------------------------------------
     samples/verify_scope_leak_free.cpp:53
     Earlier VERIFY failed
     -------------------------------------------------------------------------------
     ===============================================================================
     3 test cases selected
     
                    Sum   Critical   Non-critical
     PASSED   :       1          1              0
     FAILED   :       2          2              0