2016-11-23 54 views
1

我是Google Test和Mock框架的新手。Double免費在Google Mock

我只是試圖運行「烏龜」的例子,它是成功的。

但是,顯示錯誤消息:雙重空閒或損壞(!prev)。

MockTurtle.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 *** 

我試圖谷歌,看到幾個同樣的問題。人們說不應該把模擬當作全局變量。

但我的例子沒有使用全局變量。

請幫我解釋爲什麼會發生雙重自由。 在此先感謝!

+0

我已經添加了更多源代碼。這就是我的例子。 – quangdien

回答

0

最後一次,我建立了谷歌測試和谷歌模擬在共享庫。 發生錯誤(雙重免費)。

我試圖建立靜態庫。此錯誤不再出現。 我不知道爲什麼,但我會調查瞭解更多細節。

0

你的錯誤是你沒有向我們展示的地方。我設法補充充足的頭部和定義,讓您的例子編譯:

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

使用此生成的文件:

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的給出了健康清潔法案:

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) 
+0

我使用Eclipse來構建我的示例。我將嘗試使用makefile來構建示例。 – quangdien

0

我有同樣的問題。在我的情況下,問題是連接谷歌測試和谷歌模擬。我通過鏈接gmock解決了這個問題,沒有gtest。

我認爲這是因爲,據我所知,gmock已經靜態連接gtest:當你編譯gmock作爲一個動態庫時,gtest也是在gmock所在的同一個文件夾中生成(作爲一個靜態庫) 。我想gmock依賴於並使用這個庫。

我想,以某種方式,連接gtest兩次(靜態和動態),產生一個free()被執行兩次。