2012-10-03 46 views
3
使用谷歌測試時

當我運行下面的代碼:內存泄漏的Windows

#include "gmock/gmock.h" 
#include "gtest/gtest.h" 

#define _CRTDBG_MAP_ALLOC 
#include <crtdbg.h> 

int main(int argc, char **argv) 
{ 
    ::testing::InitGoogleTest(&argc, argv); 
    _CrtDumpMemoryLeaks(); 
    return 0; 
} 

我得到以下輸出:

Detected memory leaks! 
Dumping objects -> 
{652} normal block at 0x00074CE0, 4 bytes long. 
Data: <L> 98 4C 07 00 
{651} normal block at 0x00074C98, 12 bytes long. 
Data: <,   > 2C 03 1B 01 00 00 00 00 00 00 00 00 
{650} normal block at 0x00074C50, 8 bytes long. 
Data: <hI  > 68 49 07 00 00 00 00 00 
{649} normal block at 0x00074C10, 4 bytes long. 
Data: <t > 74 03 1B 01 
{648} normal block at 0x00074BC8, 8 bytes long. 
Data: <xK  > 78 4B 07 00 00 00 00 00 
{647} normal block at 0x00074B70, 28 bytes long. 
Data: <   K L > BC 01 1B 01 01 CD CD CD C8 4B 07 00 E0 4C 07 00 
{646} normal block at 0x00074B28, 8 bytes long. 
Data: < I  > 18 49 07 00 00 00 00 00 
{645} normal block at 0x00074AE0, 8 bytes long. 
Data: < I  > 04 49 07 00 00 00 00 00 
{644} normal block at 0x00074A98, 8 bytes long. 
Data: < H  > DC 48 07 00 00 00 00 00 
{643} normal block at 0x00074A50, 8 bytes long. 
Data: < H  > C8 48 07 00 00 00 00 00 
{642} normal block at 0x00074A08, 8 bytes long. 
Data: < H  > B4 48 07 00 00 00 00 00 
{641} normal block at 0x000749C0, 8 bytes long. 
Data: < H  > A0 48 07 00 00 00 00 00 
{640} normal block at 0x00074E90, 1 bytes long. 
Data: < > 00 
{639} normal block at 0x00074870, 272 bytes long. 
Data: <  t N > 20 03 1B 01 CD CD CD CD 74 FA 1B 01 90 4E 07 00 
{638} normal block at 0x00074F68, 72 bytes long. 
Data: <C:\Users\Baz> 43 3A 5C 55 73 65 72 73 5C 45 42 41 52 47 52 49 
{637} normal block at 0x00074E48, 8 bytes long. 
Data: <hO G > 68 4F 07 00 47 00 00 00 
{616} normal block at 0x00074EE0, 72 bytes long. 
Data: <C:\Users\Baz> 43 3A 5C 55 73 65 72 73 5C 45 42 41 52 47 52 49 
{595} normal block at 0x00074828, 8 bytes long. 
Data: <  > F0 F9 1B 01 00 00 00 00 
{594} normal block at 0x000747E8, 1 bytes long. 
Data: < > 00 
{561} normal block at 0x000747A0, 5 bytes long. 
Data: <fast > 66 61 73 74 00 
{496} normal block at 0x00074760, 1 bytes long. 
Data: < > 00 
{311} normal block at 0x00074720, 1 bytes long. 
Data: < > 00 
{282} normal block at 0x000746E0, 2 bytes long. 
Data: <* > 2A 00 
{253} normal block at 0x00074698, 5 bytes long. 
Data: <auto > 61 75 74 6F 00 
Object dump complete. 

我在做什麼錯?

回答

3

添加到接受的答案,在谷歌documentation狀態:

由於靜態初始化的谷歌測試單身人士需要在堆上,在Visual c分配++內存泄漏檢測器將在程序運行結束時報告內存泄漏。最簡單的方法是使用_CrtMemCheckpoint和_CrtMemDumpAllObjectsSince調用不報告任何靜態初始化的堆對象。有關更多詳細信息和其他堆檢查/調試例程,請參閱MSDN。

這就需要調用_CrtMemCheckPoint剛過::testing::InitGoogleTest然後RUN_ALL_TESTS()後調用_CrtMemDumpAllObjectsSince。主要功能看起來有點像這樣:

::testing::InitGoogleTest(&argc, &argv); 
// Get a checkpoint of the memory after Google Test has been initialized. 
_CrtMemState memoryState = {0}; 
_CrtMemCheckpoint(&memoryState); 
int retval = RUN_ALL_TESTS(); 

// Check for leaks after tests have run 
_CrtMemDumpAllObjectsSince(&memoryState); 
return retval; 

不幸的是,如果測試失敗谷歌測試導致內存泄漏,這意味着這不是一個完美的解決方案。

+2

不幸的是這還不夠,在GTEST-1.7.0中有未釋放RUN_ALL_TESTS內作出GTEST分配,檢查'UnitTestImpl :: os_stack_trace_getter'例如 – kerim

+1

作爲@kerim提到,僅憑_CrtMemCheckpoint做法是不足夠的,由於靜態分配在'UnitTestImpl :: os_stack_trace_getter'中執行。 我的哈克解決方法是始終在內存檢查點之前執行虛擬測試。這增加了一個額外的序言測試,但至少我可以檢查泄漏。 提示:您可以使用':: testing :: GTEST_FLAG(filter)=「DummyTestName *」'來執行虛擬測試,然後使用':: testing :: GTEST_FLAG(filter)=「-DummyTestName *」'來執行在運行其餘的時候排除它。 –