cppcheck/gui/help/ch08.html

17 lines
2.8 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Chapter 8. Exception safety</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="ch07.html" title="Chapter 7. Leaks"><link rel="next" href="ch09.html" title="Chapter 9. html report"></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 8. Exception safety</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch07.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch09.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter 8. Exception safety"><div class="titlepage"><div><div><h2 class="title"><a name="id2587519"></a>Chapter 8. Exception safety</h2></div></div></div><p>Cppcheck has a few checks that ensure that you don't break the basic
guarantee of exception safety. It doesn't have any checks for the strong
guarantee yet.</p><p>Example:</p><pre class="programlisting">Fred::Fred() : a(new int[20]), b(new int[20])
{
}</pre><p>By default cppcheck will not detect any problems in that
code.</p><p>To enable the exception safety checking you can use
<code class="literal">--enable</code>:</p><pre class="programlisting">cppcheck --enable=exceptNew --enable=exceptRealloc fred.cpp</pre><p>The output will be:</p><pre class="programlisting">[fred.cpp:3]: (style) Upon exception there is memory leak: a</pre><p>If an exception occurs when <code class="literal">b</code> is allocated,
<code class="literal">a</code> will leak.</p><p>Here is another example:</p><pre class="programlisting">int *p;
int a(int sz)
{
delete [] p;
if (sz &lt;= 0)
throw std::runtime_error("size &lt;= 0");
p = new int[sz];
}</pre><p>Check that with Cppcheck:</p><pre class="programlisting">cppcheck --enable=exceptNew --enable=exceptRealloc except2.cpp</pre><p>The output from Cppcheck is:</p><pre class="programlisting">[except2.cpp:7]: (error) Throwing exception in invalid state, p points at deallocated memory</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch07.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch09.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 7. Leaks </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 9. html report</td></tr></table></div></body></html>