确定Googletest

时间:2016-11-21 08:48:28

标签: c++ windows visual-studio-2012 memory-leaks googletest

我使用GTest 1.7在Windows 7中的Visual Studio 2012中测试C ++代码。

我已经扩展了GTest,尝试使用_CrtMem...方法进行简单的内存泄漏测试。 (来源:https://github.com/stbrenner/gtest_mem

在大多数情况下,一切都很棒!但是我目前报告了一个44字节内存泄漏的情况,我认为这可能是误报,但我无法确定如何识别它。

我必须扩展GTest的代码是:

// ------------------------------------------------------------------------------------------------
#include <gtest/gtest.h>
// ------------------------------------------------------------------------------------------------
#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
// ------------------------------------------------------------------------------------------------
namespace testing
{
    class MemoryLeakDetector : public EmptyTestEventListener
    {
#ifdef _DEBUG
    public:
        virtual void OnTestStart(const TestInfo&)
        {
            _CrtMemCheckpoint(&memState_);
        }

        virtual void OnTestEnd(const TestInfo& test_info)
        {
            if(test_info.result()->Passed())
            {
                _CrtMemState stateNow, stateDiff;
                _CrtMemCheckpoint(&stateNow);
                int diffResult = _CrtMemDifference(&stateDiff, &memState_, &stateNow);
                if (diffResult)
                {
                    FAIL() << "Memory leak of " << stateDiff.lSizes[1] << " byte(s) detected.";
                }
            }
        }

    private:
        _CrtMemState memState_;
#endif // _DEBUG
    };
}
// ------------------------------------------------------------------------------------------------
int main(int argc, char **argv) 
{
    ::testing::InitGoogleTest(&argc, argv);
    testing::UnitTest::GetInstance()->listeners().Append(new testing::MemoryLeakDetector());

    RUN_ALL_TESTS();

    system("pause");
    return 0;
}
// ------------------------------------------------------------------------------------------------

当我发现泄漏时,我正在寻找的是一种倾倒显然泄露的记忆区域的方法,以便我可以尝试识别它来自哪里。

我曾希望添加_CrtDumpMemoryLeaks();(在FAIL()之前或之后)会有所帮助,但这并不能输出额外的任何内容。

0 个答案:

没有答案
相关问题