<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[简单实践]Linux c++上常用内存泄露检测工具有valgrind， Linux C/C++ 内存泄漏检测工具：Valgrind。 ]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Sat, 13 Aug 2011 09:01:42 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	Linux c++上常用内存泄露检测工具有valgrind, Rational purify。Valgrind免费。Valgrind 可以在 32 位或 64 位 PowerPC/Linux 内核上工作。<br/>Valgrind工具包包含多个工具，如Memcheck,Cachegrind,Helgrind, Callgrind，Massif。下面分别介绍个工具的作用：<br/>Memcheck 工具主要检查下面的程序错误：<br/>• 使用未初始化的内存 (Use of uninitialised memory)<br/>• 使用已经释放了的内存 (Reading/writing memory after it has been free’d)<br/>• 使用超过 malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)<br/>• 对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)<br/>• 申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)<br/>• malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])<br/>• src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)<br/>Valgrind不检查静态分配数组的使用情况。<br/>Valgrind占用了更多的内存--可达两倍于你程序的正常使用量。如果你用Valgrind来检测使用大量内存的程序就会遇到问题，它可能会用很长的时间来运行测试<br/>2.1. 下载安装<br/>http://www.valgrind.org<br/>安装<br/>./configure;make;make install<br/>2.2. 编译程序<br/>被检测程序加入 –g&nbsp;&nbsp; -fno-inline 编译选项保留调试信息。<br/><br/>2.3. 内存泄露检测<br/>$ valgrind --leak-check=full --show-reachable=yes --trace-children=yes&nbsp;&nbsp;&nbsp;&nbsp;./iquery&nbsp;&nbsp; -f ../conf/se.conf_forum -t&nbsp;&nbsp; ~/eragon/forum_thread_data/f.log -NT&nbsp;&nbsp; -cache 0<br/>其中--leak-check=full 指的是完全检查内存泄漏，--show-reachable=yes是显示内存泄漏的地点，--trace-children=yes是跟入子进程。当程序正常退出的时候valgrind自然会输出内存泄漏的信息。 <br/><br/>来自: http://hi.baidu.com/benbendy/blog/item/905f7638a3233bfab211c709.html <br/>参看：http://zyan.cc/post/419/<br/><br/>—————————————————试着调一个epoll的多进程测试小程序————————————————————<br/>刚运行起来没有泄漏，多请求几次后再ctrl+C退出时，发现有泄漏情况了，如下所示：<br/>[root@test multepoolserver]# valgrind --tool=memcheck --leak-check=full ./multipepollserver<br/><textarea name="code" class="php" rows="15" cols="100">
[root@test multepoolserver]# valgrind --tool=memcheck --leak-check=full multipepollserver /
valgrind: multipepollserver: command not found
[root@test multepoolserver]# valgrind --tool=memcheck --leak-check=full multipepollserver
valgrind: multipepollserver: command not found
[root@test multepoolserver]# valgrind --tool=memcheck --leak-check=full multipepollserver
valgrind: multipepollserver: command not found
[root@test multepoolserver]# valgrind --tool=memcheck --leak-check=full ./multipepollserver
==24877== Memcheck, a memory error detector
==24877== Copyright (C) 2002-2013, and GNU GPL&#039;d, by Julian Seward et al.
==24877== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==24877== Command: ./multipepollserver
==24877== 
==24877== Conditional jump or move depends on uninitialised value(s)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;at 0x3C9A2436B0: vfprintf (in /lib64/libc-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x3C9A263B48: vsprintf (in /lib64/libc-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x4019A1: prename_setproctitle(char const*, ...) (multipepollserver.cpp:286)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x40298D: main (multipepollserver.cpp:481)
==24877== 
==24877== Use of uninitialised value of size 8
==24877==&nbsp;&nbsp;&nbsp;&nbsp;at 0x4A08EA2: strlen (mc_replace_strmem.c:404)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x3C9A246B68: vfprintf (in /lib64/libc-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x3C9A263B48: vsprintf (in /lib64/libc-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x4019A1: prename_setproctitle(char const*, ...) (multipepollserver.cpp:286)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x40298D: main (multipepollserver.cpp:481)
==24877== 
==24877== Use of uninitialised value of size 8
==24877==&nbsp;&nbsp;&nbsp;&nbsp;at 0x4A08EB4: strlen (mc_replace_strmem.c:404)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x3C9A246B68: vfprintf (in /lib64/libc-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x3C9A263B48: vsprintf (in /lib64/libc-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x4019A1: prename_setproctitle(char const*, ...) (multipepollserver.cpp:286)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x40298D: main (multipepollserver.cpp:481)
==24877== 
==24877== Use of uninitialised value of size 8
==24877==&nbsp;&nbsp;&nbsp;&nbsp;at 0x3C9A26E256: _IO_default_xsputn (in /lib64/libc-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x3C9A246502: vfprintf (in /lib64/libc-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x3C9A263B48: vsprintf (in /lib64/libc-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x4019A1: prename_setproctitle(char const*, ...) (multipepollserver.cpp:286)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x40298D: main (multipepollserver.cpp:481)
==24877== 
parent process id 24877
==24877== Warning: ignored attempt to set SIGKILL handler in sigaction();
==24877==&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the SIGKILL signal is uncatchable
child process id 24878,parent id 24877
==24878== Warning: ignored attempt to set SIGKILL handler in sigaction();
==24878==&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the SIGKILL signal is uncatchable
进入线程举旗：sync_additional_writing_worker...
==24878== Conditional jump or move depends on uninitialised value(s)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;at 0x3C9A2436B0: vfprintf (in /lib64/libc-2.5.so)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x3C9A263B48: vsprintf (in /lib64/libc-2.5.so)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x4019A1: prename_setproctitle(char const*, ...) (multipepollserver.cpp:286)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x402B93: main (multipepollserver.cpp:558)
==24878== 

thread id is 94632256,procees id is 24877,waiting for into while...
==24878== Warning: invalid file descriptor -1 in syscall close()
==24878== Syscall param epoll_ctl(event) points to uninitialised byte(s)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;at 0x3C9A2D3F9A: epoll_ctl (in /lib64/libc-2.5.so)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x401DAA: Process(int, int*) (multipepollserver.cpp:102)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x402BA0: main (multipepollserver.cpp:560)
==24878==&nbsp;&nbsp;Address 0xfff000688 is on thread 1&#039;s stack


浏览器多请求几次，于是出现了 definitely lost，如下：
==24877== 
==24877== HEAP SUMMARY:
==24877==&nbsp;&nbsp;&nbsp;&nbsp; in use at exit: 1,901 bytes in 31 blocks
==24877==&nbsp;&nbsp; total heap usage: 32 allocs, 1 frees, 2,469 bytes allocated
==24877== 
==24878== 
==24878== HEAP SUMMARY:
==24878==&nbsp;&nbsp;&nbsp;&nbsp; in use at exit: 1,906 bytes in 60 blocks
==24878==&nbsp;&nbsp; total heap usage: 61 allocs, 1 frees, 2,474 bytes allocated
==24878== 
==24878== 293 (240 direct, 53 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 4
==24878==&nbsp;&nbsp;&nbsp;&nbsp;at 0x4A072D0: malloc (vg_replace_malloc.c:291)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x401AF4: prename_setproctitle_init(int, char**, char**) (multipepollserver.cpp:261)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x402B80: main (multipepollserver.cpp:557)
==24878== 
==24878== 1,613 (240 direct, 1,373 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==24878==&nbsp;&nbsp;&nbsp;&nbsp;at 0x4A072D0: malloc (vg_replace_malloc.c:291)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x401AF4: prename_setproctitle_init(int, char**, char**) (multipepollserver.cpp:261)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x40297A: main (multipepollserver.cpp:480)
==24878== 
==24878== LEAK SUMMARY:
==24878==&nbsp;&nbsp;&nbsp;&nbsp;definitely lost: 480 bytes in 2 blocks
==24878==&nbsp;&nbsp;&nbsp;&nbsp;indirectly lost: 1,426 bytes in 58 blocks
==24878==&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;possibly lost: 0 bytes in 0 blocks
==24878==&nbsp;&nbsp;&nbsp;&nbsp;still reachable: 0 bytes in 0 blocks
==24878==&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; suppressed: 0 bytes in 0 blocks
==24878== 
==24878== For counts of detected and suppressed errors, rerun with: -v
==24878== Use --track-origins=yes to see where uninitialised values come from
==24878== ERROR SUMMARY: 41 errors from 8 contexts (suppressed: 4 from 4)
==24877== 288 bytes in 1 blocks are possibly lost in loss record 1 of 3
==24877==&nbsp;&nbsp;&nbsp;&nbsp;at 0x4A0652F: calloc (vg_replace_malloc.c:618)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x3C9920FEF2: _dl_allocate_tls (in /lib64/ld-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x3C9AE06C29: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.5.so)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x402A36: main (multipepollserver.cpp:512)
==24877== 
==24877== 1,613 (240 direct, 1,373 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3
==24877==&nbsp;&nbsp;&nbsp;&nbsp;at 0x4A072D0: malloc (vg_replace_malloc.c:291)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x401AF4: prename_setproctitle_init(int, char**, char**) (multipepollserver.cpp:261)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x40297A: main (multipepollserver.cpp:480)
==24877== 
==24877== LEAK SUMMARY:
==24877==&nbsp;&nbsp;&nbsp;&nbsp;definitely lost: 240 bytes in 1 blocks
==24877==&nbsp;&nbsp;&nbsp;&nbsp;indirectly lost: 1,373 bytes in 29 blocks
==24877==&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;possibly lost: 288 bytes in 1 blocks
==24877==&nbsp;&nbsp;&nbsp;&nbsp;still reachable: 0 bytes in 0 blocks
==24877==&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; suppressed: 0 bytes in 0 blocks
==24877== 
==24877== For counts of detected and suppressed errors, rerun with: -v
==24877== Use --track-origins=yes to see where uninitialised values come from
==24877== ERROR SUMMARY: 21 errors from 6 contexts (suppressed: 4 from 4)
[root@test multepoolserver]# 


————————————————返回结果中的“definitely lost: 480 bytes in 2 blocks”表示发生内存泄漏—————————————————
重点性查一下,multipepollserver.cpp:261 行,main (multipepollserver.cpp:512)：

==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x401AF4: prename_setproctitle_init(int, char**, char**) (multipepollserver.cpp:261)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x402B80: main (multipepollserver.cpp:557)
==24878== 
==24878== 1,613 (240 direct, 1,373 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==24878==&nbsp;&nbsp;&nbsp;&nbsp;at 0x4A072D0: malloc (vg_replace_malloc.c:291)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x401AF4: prename_setproctitle_init(int, char**, char**) (multipepollserver.cpp:261)
==24878==&nbsp;&nbsp;&nbsp;&nbsp;by 0x40297A: main (multipepollserver.cpp:480)
==24878== 
==24878== LEAK SUMMARY:
==24878==&nbsp;&nbsp;&nbsp;&nbsp;definitely lost: 480 bytes in 2 blocks

==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x402A36: main (multipepollserver.cpp:512)
==24877== 
==24877== 1,613 (240 direct, 1,373 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3
==24877==&nbsp;&nbsp;&nbsp;&nbsp;at 0x4A072D0: malloc (vg_replace_malloc.c:291)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x401AF4: prename_setproctitle_init(int, char**, char**) (multipepollserver.cpp:261)
==24877==&nbsp;&nbsp;&nbsp;&nbsp;by 0x40297A: main (multipepollserver.cpp:480)
==24877== 
==24877== LEAK SUMMARY:
==24877==&nbsp;&nbsp;&nbsp;&nbsp;definitely lost: 240 bytes in 1 blocks
</textarea><br/><br/>一查原因，两条遗漏：<br/>for (i = 0; envp[i] != NULL; i++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;environ[i] = strdup(envp[i]);//xstrdup(envp[i]);<br/>environ[i] = NULL;<br/>1.这个for没有把：environ[i] = NULL; 给括号进来，没有释放掉？不对：<br/>strdup<br/>(gdb) print *envp<br/>$2 = 0x7fff72df6afa &quot;HOSTNAME=test.local&quot;<br/>$4 = 0x7fff72df6ac8 &quot;/home/xiangdong/multepoolserver/multipepollserver&quot;<br/>2.没有free掉char *：char * httpmut_prename_child_buf=&quot;worker process&quot;; /* 原命令行参数:子进程名字。 */ <br/>prename_setproctitle(&quot;[httpmut: worker process] %s&quot;, httpmut_prename_child_buf);<br/>free(httpmut_prename_child_buf);//这个得free掉。<br/><br/>==26827==&nbsp;&nbsp;&nbsp;&nbsp;by 0x4029CA: main (multipepollserver.cpp:484)<br/>(gdb) c<br/>Continuing.<br/><br/>Program received signal SIGSEGV, Segmentation fault.<br/>0x0000003c9a27272e in free () from /lib64/libc.so.6<br/>(gdb) bt<br/>#0&nbsp;&nbsp;0x0000003c9a27272e in free () from /lib64/libc.so.6<br/>#1&nbsp;&nbsp;0x00000000004029e7 in main (argc=1, argv=0x7fff72df4e08, envp=0x7fff72df4e18) at multipepollserver.cpp:486<br/><br/><br/>用ab.exe压力一下？<br/>D:&#92;wamp&#92;bin&#92;apache&#92;apache2.2.22&#92;bin&#92;ab.exe -c 1000 -n 1000 http://t.jackxiang.com<br/>会提示：[http://]hostname[:port]/path<br/>D:&#92;wamp&#92;bin&#92;apache&#92;apache2.2.22&#92;bin&#92;ab.exe -c 1000 -n 1000 http://t.jackxiang.com/<br/><br/>C语言调指针可真是个小心活儿，还得有工具，有经验啊EOF。<br/>
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [简单实践]Linux c++上常用内存泄露检测工具有valgrind， Linux C/C++ 内存泄漏检测工具：Valgrind。 ]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>