gtest 的彩色資訊輸出 + boost.test 的記憶體流失檢測及定位

來源:互聯網
上載者:User

gtest  的彩色資訊輸出 + boost.test 的記憶體流失檢測及定位 

/* Author : Andrew.Wu    [ Created on : 2009/04/29 - Last Modified On  : 2009/05/02
*/
 
gtest 的彩色資訊顯示看起來比較醒目,boost。test 的記憶體流失檢查很實用。真希望兩個功能在一個庫裡,可以省不少事情。不過如果想在作者們沒有這麼做之前結合使用這兩個功能,下面的方法可以試。

 
1: #include <iostream>
2:
3: #include <gtest/gtest.h>
4: #pragma comment(lib, "gtestd_d.lib")
5:
6: #include <boost/test/unit_test.hpp>
7:
8: TEST(FooTest, test)
9: {
10: char *t = new char[2];
11: EXPECT_EQ(3, 3);
12: EXPECT_EQ(2, 3);
13: }
14:
15: using boost::unit_test::test_case;
16: void testf()
17: {
18: char *t = new char[2];
19: BOOST_CHECK(3 == 3);
20: BOOST_CHECK(2 == 3);
21: }
22:
23: using boost::unit_test::test_suite;
24: test_suite* init_unit_test_suite( int argc, char* argv[] )
25: {
26: std::cout << " gtest...\n";
27: ::testing::InitGoogleTest(&argc, argv);
28: ::testing::UnitTest::GetInstance()->Run();
29:
30: std::cout << "\n\n boost.test...\n";
31: test_suite* ts = BOOST_TEST_SUITE( "" );
32: ts->add( BOOST_TEST_CASE( &testf ) );
33: return ts;
34: }

 
(具體使用 test case 時只用 gtest 的即可,裡面兩者都寫僅僅是為了有個對比)
 
gtest 1.3.0
http://code.google.com/p/googletest/
booost 1.8.0
http://www.boost.org/
 
output: 

 
 
windows mfc 程式定位執行個體 :
情境1:

Detected memory leaks!
Dumping objects ->
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {1081} normal block
 at 0x0038F448, 30 bytes long.
 Data: <   x            > 0C 00 E5 78 06 00 00 00 06 00 00 00 01 00 00 00
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\inet.cpp(509) : {1080} client block at
 0x01268148, subtype c0, 28 bytes long.
a CHttpConnection object at $01268148, 28 bytes long
Object dump complete.
 
--
inet.cpp(509) : {1080}
inet.cpp 的第 509 行分配的記憶體
specified object allocation order number 1080
--
 
找到本機對應的檔案
C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc\inet.cpp
 
找到相應的行
CHttpConnection* CInternetSession::GetHttpConnection(LPCTSTR pstrServer,
 INTERNET_PORT nPort /* = INTERNET_INVALID_PORT_NUMBER */,
 LPCTSTR pstrUserName /* = NULL */, LPCTSTR pstrPassword /* = NULL */)
{
 ASSERT(AfxIsValidString(pstrServer));
       // line 509
 CHttpConnection* pResult = new CHttpConnection(this,
  pstrServer, nPort, pstrUserName, pstrPassword, m_dwContext);
 return pResult;
}
 
如此應該是有這個函數的調用者沒有清理返回的 CHttpConnection*,搜尋調用代碼發現果然如此
 
情境2:
記憶體流失檢測資訊中沒有檔案名稱及行號
Detected memory leaks!
Dumping objects ->
{1080} normal block at 0x00C28148, 1 bytes long.
 Data: < > CD
Object dump complete.
 
 
在程式的開始加入 _CrtSetBreakAlloc(1080);
--

 
Start debugging
在此記憶體配置的位置(但不精確)會停下來提示

xxx.exe has triggered a breakpoint

當然,還是有檔案名稱與行號省事,試試在檔案適當位置加入
 
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//#define DEBUG_NEW new(THIS_FILE, __LINE__)
 
如此就可以象上面一樣看到檔案名稱及行號
 
Detected memory leaks!
Dumping objects ->
d:\work\xxx\xxx.cpp(167)
 : {1080} normal block at 0x00C28148, 1 bytes long.
 Data: < > CD
Object dump complete.
 
 
 
Sets a breakpoint on a specified object allocation order number (debug version only).
--

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.