c++ - GMock leaks memory on 'Uninteresting mock function calls' since update - Stack Overflow

Since updating GTest framework from 1.8.x to 1.12.x, there have been a few tests that report memory lea

Since updating GTest framework from 1.8.x to 1.12.x, there have been a few tests that report memory leaks at the end of the test.

I've stripped out all the complexity of our setups to the example below and tested it on the old framework and the new. This sample does not show memory leaks in the old, it now does. It seems to be triggered by a function call made on the mock for which there isn't an explicit EXPECT.

Uncommenting the expectation line makes the memory leak go away.

The mock in our original code was a static shared_ptr, but making it a class member, or stack variable in the test, don't fix the issue.

The offending allocation appears to be the instantiation of an unordered_map inside 'UninterestingCallReactionMap()' in gmock-spec-builders

using namespace testing;

class IFoo
{
public:

    IFoo() = default;
    virtual ~IFoo() = default;

    virtual bool closeLink(void) = 0;
};

class MockFoo : public IFoo
{
public:
    MOCK_METHOD0(closeLink, bool());
};

class MemLeakReproTest : public Test
{
};

TEST_F(MemLeakReproTest, TestStuff)
{
    MockFoo mockFoo;

    //EXPECT_CALL(mockFoo, closeLink()).Times(1).WillOnce(Return(true));
    mockFoo.closeLink();
}

Edit: added in response to the question about how we are detecting the leaks.

This is in the main.cpp of the test project (running on Windows). Is our setup in main correct?

{
 // Enable leak checking on exit
 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
 // Write CRT warnings to stderr
 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);

 wchar_t filename[MAX_PATH] = {};
 wcscpy_s(filename, *wargv);
 wcscat_s(filename, L".memleaks");

 hLogFile = CreateFile(filename, GENERIC_WRITE,
     FILE_SHARE_READ, NULL, CREATE_ALWAYS,
     FILE_ATTRIBUTE_NORMAL, NULL);

 _CrtSetReportHook(reportHook);

 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);

 testing::InitGoogleTest(&argc, argv);
 int result = RUN_ALL_TESTS();

 LocalFree(wargv);

 return result;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745302071a4621494.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信