0

我正在学习 boost 单元测试,我很高兴地发现它可以检测内存泄漏,所以我正在测试它。我创建了以下可怕的方法:

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);  
}

我显然在这里创建了 2 个内存泄漏。为什么测试人员不关心?我的测试以优异的成绩通过。

我不想修改实际代码,正如我在这里看到的:http: //www.boost.org/doc/libs/1_35_0/libs/test/example/exec_mon_example.cpp

为什么我没有收到错误消息?

4

1 回答 1

0

我不确定提升,但要让 Visual Studio 的调试堆管理器工作,你必须写这样的东西:

#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
}

因为它泄露了 DEBUG 版本的输出,所以看起来像这样:

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).

可能 boost 使用相同的东西来检测内存泄漏。

RELEASE 版本不检测内存泄漏,因为 Visual Studio 的“调试堆管理器”在 RELEASE 版本中不起作用。您认为为什么他们将其命名为“调试堆管理器”?

于 2012-10-12T17:39:57.193 回答