惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 吴尔平

在低版本的 vc 中使用 vc 10.0 的新特性 使用另一个blog: http://blog.csdn.net/WuErPing scons + swig 如何在 vista 使用 Device Emulator 连接internet vc9 Feature Pack Beta tr1 的一些问题 NSIS Kill Process (转贴) C#与一个彩票页面 - 吴尔平 - 博客园 py2exe 转换 pytetris - 吴尔平 关于模板化的friend class C# 的 random shuffle python 读取 windows event log 的简短代码 IronPython 1.0 Release Candidate (转贴 ( 据说比c实现的快1.5!) ) Visual Studio Service Pack (转贴) The 16th Annual Jolt Product Excellence Award Winners (转贴) C++/CLI FAQ (逐步整理中) C++/CLI singleton模式 (双重检测锁实现) 如何在 VS2005 的 Team Unit Testing frameworks 中测试 Native Code (C++ ) 2005 CRT memory leaks 改变 SQL Server 2000 所有对象的所有者
gtest 的彩色信息输出 + boost.test 的内存泄漏检测及定位
吴尔平 · 2009-06-09 · via 博客园 - 吴尔平

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).
--