在Google模拟中双免费
Double free in Google Mock
我是Google测试和模拟框架中的新手。
我只是尝试运行"乌龟"例子,这是成功的。
但是,显示错误消息:双免费或损坏(!prev)。
oigturtle.h
#include <gmock/gmock.h>
class MockTurtle : class Turtle {
MOCK_METHOD0(PenUp, void());
MOCK_METHOD0(PenDown, void());
MOCK_METHOD1(Forward, void(int distance));
MOCK_METHOD1(Turn, void(int degrees));
MOCK_METHOD2(GoTo, void(int x, int y));
MOCK_CONST_METHOD0(GetX, int());
MOCK_CONST_METHOD0(GetY, int());
};
Turtle.h
class Turtle {
virtual ~Turtle() {}
virtual void PenUp() = 0;
virtual void PenDown() = 0;
virtual void Forward(int distance) = 0;
virtual void Turn(int degrees) = 0;
virtual void GoTo(int x, int y) = 0;
virtual int GetX() const = 0;
virtual int GetY() const = 0;
};
Painter.h
class Painter
{
Turtle* turtle;
public:
Painter( Turtle* turtle )
: turtle(turtle){}
bool DrawCircle(int, int, int){
turtle->PenDown();
return true;
}
};
main_test.cpp
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "Painter.h"
#include "MockTurtle.h"
using ::testing::AtLeast;
TEST(PainterTest, CanDrawSomething) {
MockTurtle turtle;
EXPECT_CALL(turtle, PenDown())
.Times(AtLeast(1));
Painter painter(&turtle);
EXPECT_TRUE(painter.DrawCircle(0, 0, 10));
}
int main(int argc, char** argv) {
// The following line must be executed to initialize Google Mock
// (and Google Test) before running the tests.
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
结果
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from PainterTest
[ RUN ] PainterTest.CanDrawSomething
[ OK ] PainterTest.CanDrawSomething (0 ms)
[----------] 1 test from PainterTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
*** Error in `/home/user/workspace/google_mock_2/Debug/google_mock_2': double free or corruption (!prev): 0x098a8080 ***
我尝试使用Google,看到了几个相同的问题。人们说,这不应将模拟用作全局变量。
但是我的示例没有使用全局变量。
请帮助我解释为什么双重自由发生。预先感谢!
上次,我在共享库中构建了Google Test和Google模拟。
发生了错误(双免费)。我试图构建为静态库。此错误不再出现。我不知道为什么,但是我会调查以了解更多细节。
我也有同样的问题。就我而言,问题是要链接Google测试和Google Mock。我仅通过链接gmock,没有gtest来解决问题。
我认为这是因为据我所知,Gmock已经链接了静态的GTEST:当您将Gmock编译成动态库时,GTEST也会在同一文件夹中生成gtest(作为静态库),在该文件夹中构建了Gomock。我想Gmock取决于和使用此库。
我想,以某种方式将GTEST链接两次(无论是静态还是动态),会产生一个免费的(),要执行两次。
对于您的信息,这是gtest/gmock中的一个错误,用于cmake共享库的构建。该拉的请求已经解决了。有关更多详细信息,请阅读第930期中的讨论。
您的错误是您没有显示我们的地方。我设法添加了足够的标题和定义,以使您的示例进行编译:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class Turtle {
public:
virtual ~Turtle() = default;
virtual void PenDown() = 0;
};
class MockTurtle : public Turtle {
public:
MOCK_METHOD0(PenDown, void());
};
class Painter
{
Turtle *turtle;
public:
Painter(Turtle *turtle)
: turtle(turtle)
{}
bool DrawCircle(int, int, int)
{
turtle->PenDown();
return true;
}
};
TEST(PainterTest, CanDrawSomething)
{
MockTurtle turtle;
EXPECT_CALL(turtle, PenDown()).Times(testing::AtLeast(1));
Painter painter(&turtle);
EXPECT_TRUE(painter.DrawCircle(0, 0, 10));
}
使用此makefile:
VPATH += $(GTEST_DIR)/src
VPATH += $(GMOCK_DIR)/src
gtest%.o: CPPFLAGS += -I$(GTEST_DIR)
gmock%.o: CPPFLAGS += -I$(GMOCK_DIR)
40762798: CXXFLAGS += -isystem $(GTEST_DIR)/include -isystem $(GMOCK_DIR)
40762798: CXXFLAGS += -pthread -Wno-effc++
40762798: LDLIBS += -lpthread
40762798: CC=g++
40762798: 40762798.o gtest_main.o gtest-all.o gmock-all.o
然后我没有任何错误,瓦尔格林德给了一个清晰的健康账单:
valgrind ./40762798
==24351== Memcheck, a memory error detector
==24351== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==24351== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==24351== Command: ./40762798
==24351==
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from PainterTest
[ RUN ] PainterTest.CanDrawSomething
[ OK ] PainterTest.CanDrawSomething (84 ms)
[----------] 1 test from PainterTest (92 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (125 ms total)
[ PASSED ] 1 test.
==24351==
==24351== HEAP SUMMARY:
==24351== in use at exit: 0 bytes in 0 blocks
==24351== total heap usage: 183 allocs, 183 frees, 117,387 bytes allocated
==24351==
==24351== All heap blocks were freed -- no leaks are possible
==24351==
==24351== For counts of detected and suppressed errors, rerun with: -v
==24351== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
相关文章:
- 如何使用Google Mock来模拟gettimeofday()
- G锁定铸造到基础上会释放模拟行为
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 落砂模拟碰撞检测C++和SFML
- 在gtest.中使用fff.h模拟系统API
- 谷歌模拟和覆盖关键字
- 用C#中的并集模拟C++嵌套结构
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 在模拟器中使用并集来模拟CPU寄存器有多合适
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何模拟不同边数的骰子滚动?
- 模拟持久按键
- 使用SIR模型的疾病爆发模拟
- 如何在cpp中删除(免费)多维结构内存?
- 在 c++ 中模拟输入并在 JAVA 中读取它?
- 存储在堆分配向量中的指针的免费存储
- 转发变量参数列表以模拟 std::thread
- 如何在谷歌模拟中匹配 C 样式数组
- 在Google模拟中双免费
- 使用谷歌模拟模拟免费功能