2012-10-12 95 views
0

我正在學習有關升壓單元測試,我發現,令人高興的是,它可以檢測內存泄漏,所以我測試它。我創建了以下方法可怕:升壓單元測試不會失敗

int ForTest::Compare(const ForTest item) 
{ 
    ForTest* existing_item = this; 
    char* x=new char[1024]; 
    m_name = std::string(x); 
    if (existing_item->m_count * existing_item->m_price == item.m_count * item.m_price) return 0; 
    if (existing_item->m_count * existing_item->m_price > item.m_count * item.m_price) return 1;  
    return -1; 
} 
BOOST_AUTO_TEST_CASE(a_test_case) 
{ 
    BOOST_TEST_CHECKPOINT("weird..."); 

    ForTest alpha("Pen", 4, 4.3); 
    ForTest beta; 

    BOOST_CHECK_EQUAL(alpha.Compare(beta), 1); 
} 

我明明在這裏創造200個內存泄漏。爲什麼測試者不關心?我的測試通過了大膽的色彩。

我不希望有修改實際的代碼,因爲我在這裏看到:http://www.boost.org/doc/libs/1_35_0/libs/test/example/exec_mon_example.cpp

爲什麼我沒有得到一個錯誤?

+0

哪個平臺,你運行的? Boost執行監視器內存泄漏檢測僅適用於某些平臺。 –

+0

Visual Studio 2010中,窗戶 – Thalia

回答

0

我不知道提振,但得到的Visual Studio調試堆管理工作,你必須寫類似的東西:

#include <crtdbg.h> 

#ifdef _DEBUG 
static char THIS_FILE[] = __FILE__; 
#define new new(_NORMAL_BLOCK, THIS_FILE, __LINE__) 
#endif 

int main() 
{ 
    _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)); 
    new int(2036427631); // deliberate leak 
} 

由於泄漏的調試版本的輸出看起來類似的東西:

Detected memory leaks! 
Dumping objects -> 
d:\fun\try\try.cpp(11) : {66} normal block at 0x00345C40, 4 bytes long. 
Data: <okay> 6F 6B 61 79 
Object dump complete. 
The program '[3216] try.exe: Native' has exited with code 0 (0x0). 

可能提升使用非常相同的東西來檢測內存泄漏。

發行版本不檢測內存泄漏原因是Visual Studio的「調試堆管理器」不會在發佈版本。你認爲他們爲什麼將其命名爲「調試堆管理器」?

+0

謝謝,回答有關的事實,測試並不在發佈工作我的問題。不過,我仍然試圖製作Debug配置的副本,但未成功。我想我可以標記你的答案作爲解決方案,並問一個單獨的問題,爲什麼/怎麼不工作,因爲我將無法建立測試任何其他方式 - 調試必須能夠運行實際可執行代碼。 – Thalia

+0

@Michaela嘗試生產最低限度比如單元測試,不爲你工作。編譯的東西。在這個過程中,您很可能會找到解決方案。 –

+0

我所有的其他單元測試工作。 (要麼通過要麼失敗)。他們都編譯。他們給出正確的預期結果。唯一不起作用的是創建一個Debug配置,這會產生內存泄漏。請看上面的例子。該測試不應該通過。它的確如此。 – Thalia