23 lines
3.3 KiB
HTML
23 lines
3.3 KiB
HTML
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Chapter 7. Leaks</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.1"><link rel="home" href="index.html" title="Cppcheck 1.44"><link rel="up" href="index.html" title="Cppcheck 1.44"><link rel="prev" href="ch06.html" title="Chapter 6. Suppressions"><link rel="next" href="ch08.html" title="Chapter 8. Exception safety"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 7. Leaks</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch08.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter 7. Leaks"><div class="titlepage"><div><div><h2 class="title"><a name="id2587421"></a>Chapter 7. Leaks</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="ch07.html#id2587433">Userdefined allocation/deallocation functions</a></span></dt></dl></div><p>Looking for memory leaks and resource leaks is a key feature of
|
||
Cppcheck. Cppcheck can detect many common mistakes by default. But through
|
||
some tweaking you can improve the checking.</p><div class="section" title="Userdefined allocation/deallocation functions"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2587433"></a>Userdefined allocation/deallocation functions</h2></div></div></div><p><code class="literal">Cppcheck</code> understands many common allocation and
|
||
deallocation functions. But not all.</p><p>Here is example code that might leak memory or resources:</p><pre class="programlisting">void foo(int x)
|
||
{
|
||
void *f = CreateFred();
|
||
if (x == 1)
|
||
return;
|
||
DestroyFred(f);
|
||
}</pre><p>If you analyse that with Cppcheck it won't find any leaks:</p><pre class="programlisting">cppcheck --enable=possibleError fred1.cpp</pre><p>You can add some custom leaks checking by providing simple
|
||
implementations for the allocation and deallocation functions. Write
|
||
this in a separate file:</p><pre class="programlisting">void *CreateFred()
|
||
{
|
||
return malloc(100);
|
||
}
|
||
|
||
void DestroyFred(void *p)
|
||
{
|
||
free(p);
|
||
}</pre><p>When Cppcheck see this it understands that CreateFred will return
|
||
allocated memory and that DestroyFred will deallocate memory.</p><p>Now, execute <code class="literal">Cppcheck</code> this way:</p><pre class="programlisting">cppcheck --append=fred.cpp fred1.cpp</pre><p>The output from cppcheck is:</p><pre class="programlisting">Checking fred1.cpp...
|
||
[fred1.cpp:5]: (error) Memory leak: f</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch08.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 6. Suppressions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 8. Exception safety</td></tr></table></div></body></html>
|