Google模拟全球模拟对象内存泄漏 [英] Google mock global mock object memory leak

查看:346
本文介绍了Google模拟全球模拟对象内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS2005和C ++进行单元测试使用google模拟。

我在单元测试中有一个全局自由函数,我使用下面的代码来模拟自由功能:

I am using VS2005, and C++ for unit testing using google mock.
I had a global free function in unit testing, and I used the following code to mock free function:

NiceMock <MockA> mockObj;  



struct IFoo {  
    virtual A* foo() = 0;  
    virtual ~IFoo() {}  
};  

struct FooMock : public IFoo {  
    FooMock() {}  
    virtual ~FooMock() {}  
    MOCK_METHOD0(foo, A*());  
};

FooMock fooMock;

// foo() implementation  
A* foo() {  
    return fooMock.foo();  
}  

SetUp c>函数,我对全局对象设置期望,如

In the SetUp() function, I set Expectations on the global object like

EXPECT_CALL(fooMock,foo())  
    .Times(1)  
    .WillOnce(Return(&mockObj));  

TEST(..., instA) {

    // ...
}

并在 TearDown()中,我删除了全局模拟对象 fooMock

and in TearDown(), I delete the global mock object fooMock

virtual TearDown(){  
    delete &fooMock;  
}  



当我运行代码时,得到以下错误

When I run the code, I get the following error


错误:内存泄漏在xyz.instA,

Error: Memory Leak in xyz.instA,



0个空闲块中的0个字节。

-1个正常块中的-61个字节。

7个CRT块中的68个字节。

0字节在0中忽略块。

0个客户端块中的0个字节。

最大数量:11025字节

分配总数:50602字节。

also,
0 bytes in 0 Free Blocks.
-61 bytes in -1 Normal Blocks.
68 bytes in 7 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 11025 byte
Total allocations: 50602 bytes.

任何人都可以告诉我这里发生了什么?如果我不删除 fooMock ,我得到错误fooMock应该删除,但永远不是,或检测到堆损坏。

从错误,我可以看到,在某处我的堆被错误处理,但我找不到点。我已经尝试一步一步调试它。

Can anyone tell me what is happening here? If I don't delete fooMock, I get the error "fooMock should be delete but never is", or Heap corruption detected.
From the error, I can see that somewhere my heap is being mishandled, but I cannot find the point. I have tried to debug it step by step as well.

一些帮助将是真的很棒! :)

Some help would be really great! :)

推荐答案

看起来问题是你实例化一个FooMock的全局实例。

It looks like the problem is that you're instantiating a global instance of FooMock. Googlemock/googletest expect the mock to be defined either within the body of the test, or within a test fixture class.

在上面的例子中,你只需要实例化fooMock内测试:

In the example above, you'd simply instantiate fooMock inside the test:

TEST(..., instA) {

    FooMock fooMock;
    // ...
}

这篇关于Google模拟全球模拟对象内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆