简体   繁体   中英

boost Unit test doesn't fail

I am learning about boost unit tests, I found out, happily, that it can detect memory leaks, so I am testing it. I created the following horrible method:

int ForTest::Compare(const ForTest item)
{
    ForTest* existing_item = this;
    char* x=new char[1024];
    m_name = std::string(x);
    if (existing_item->m_count * existing_item->m_price == item.m_count * item.m_price) return 0;
    if (existing_item->m_count * existing_item->m_price > item.m_count * item.m_price) return 1;    
    return -1;
}
BOOST_AUTO_TEST_CASE( a_test_case)
{
    BOOST_TEST_CHECKPOINT("weird...");

    ForTest alpha("Pen", 4, 4.3);
    ForTest beta;

    BOOST_CHECK_EQUAL(alpha.Compare(beta), 1);  
}

I am obviously creating 2 memory leaks here. Why doesn't the tester care ? My test passes with flying colors.

I don't want to have to modify actual code, as I saw here: http://www.boost.org/doc/libs/1_35_0/libs/test/example/exec_mon_example.cpp

Why am I not getting an error ?

I am not sure about boost but to get debug heap manager of Visual Studio to work you have to write something like that:

#include <crtdbg.h>

#ifdef _DEBUG
static char THIS_FILE[] = __FILE__;
#define new new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
#endif

int main()
{
    _CrtSetDbgFlag( _CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) );
    new int(2036427631); // deliberate leak
}

Since it leaks the output of the DEBUG version will look something like that:

Detected memory leaks!
Dumping objects ->
d:\fun\try\try.cpp(11) : {66} normal block at 0x00345C40, 4 bytes long.
 Data: <okay> 6F 6B 61 79 
Object dump complete.
The program '[3216] try.exe: Native' has exited with code 0 (0x0).

Probably boost uses very same thing to detect the memory leaks.

The RELEASE version does not detect memory leaks because the "debug heap manager" of Visual Studio does not work in RELEASE version. What you think why they named it as "debug heap manager"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM