1 2 3 4 5 6 7 8 9 10 |
<span style="color: #339933;">#include &lt;mcheck.h&gt;</span> <span style="color: #339933;">#include &lt;stdio.h&gt;</span> <span style="color: #339933;">#include &lt;stdlib.h&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()查看内存泄漏