最新消息:

用mtrace()查看内存泄漏

C/C++ admin 3547浏览 0评论
1
2
3
4
5
6
7
8
9
10
<span style="color: #339933;">#include &amp;lt;mcheck.h&amp;gt;</span>
<span style="color: #339933;">#include &amp;lt;stdio.h&amp;gt;</span>
<span style="color: #339933;">#include &amp;lt;stdlib.h&amp;gt;</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">(</span><span style="color: #993333;">void</span><span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
    setenv<span style="color: #009900;">(</span><span style="color: #ff0000;">"MALLOC_TRACE"</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">"./memleak.log"</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    mtrace<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>p<span style="color: #339933;">=</span>malloc<span style="color: #009900;">(</span><span style="color: #0000dd;">10</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

然后用mtrace命令就可以查看内存泄漏情况:

1
$ mtrace memleak.log

转载请注明:爱开源 » 用mtrace()查看内存泄漏

您必须 登录 才能发表评论!