fixed output memory leak

This commit is contained in:
mes5k 2005-01-04 20:21:20 +00:00
parent 8497556541
commit 3c4771b03c
3 changed files with 22 additions and 4 deletions

View File

@ -591,7 +591,10 @@ int main(int argc, char** argv)
</pre><p> </pre><p>
See <tt class="filename">test4.cpp</tt> in the examples directory for the full See <tt class="filename">test4.cpp</tt> in the examples directory for the full
example. example. <span class="emphasis"><em>NOTE</em></span>: if you supply your own Output object, we
will not delete it in the <tt class="classname">CmdLine</tt> destructor. This
could lead to a (very small) memory leak if you don't take care of the object
yourself.
</p></div></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="EXCEPTIONS"></a>Chapter 3. Exceptions to the Rules</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><a href="#IGNORE_ARGS">Ignoring arguments</a></dt><dt><a href="#COMBINED_SWITCHES">Multiple Identical Switches</a></dt><dt><a href="#DESCRIPTION_EXCEPTIONS">Type Descriptions</a></dt></dl></div><p> </p></div></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="EXCEPTIONS"></a>Chapter 3. Exceptions to the Rules</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><a href="#IGNORE_ARGS">Ignoring arguments</a></dt><dt><a href="#COMBINED_SWITCHES">Multiple Identical Switches</a></dt><dt><a href="#DESCRIPTION_EXCEPTIONS">Type Descriptions</a></dt></dl></div><p>
Like all good rules, there are many exceptions.... Like all good rules, there are many exceptions....
</p><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="IGNORE_ARGS"></a>Ignoring arguments</h2></div></div><div></div></div><p> </p><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="IGNORE_ARGS"></a>Ignoring arguments</h2></div></div><div></div></div><p>

View File

@ -750,7 +750,10 @@ int main(int argc, char** argv)
</programlisting> </programlisting>
See <filename>test4.cpp</filename> in the examples directory for the full See <filename>test4.cpp</filename> in the examples directory for the full
example. example. <emphasis>NOTE</emphasis>: if you supply your own Output object, we
will not delete it in the <classname>CmdLine</classname> destructor. This
could lead to a (very small) memory leak if you don't take care of the object
yourself.
</para> </para>
</sect1> </sect1>
</chapter> </chapter>

View File

@ -136,6 +136,12 @@ class CmdLine : public CmdLineInterface
*/ */
void deleteOnExit(Visitor* ptr); void deleteOnExit(Visitor* ptr);
/**
* Is set to true when a user sets the output object. We use this so
* that we don't delete objects that are created outside of this lib.
*/
bool _userSetOutput;
public: public:
/** /**
@ -258,7 +264,8 @@ inline CmdLine::CmdLine(const std::string& n,
_message(m), _message(m),
_version(v), _version(v),
_numRequired(0), _numRequired(0),
_delimiter(' ') _delimiter(' '),
_userSetOutput(false)
{ {
_constructor(); _constructor();
} }
@ -270,7 +277,8 @@ inline CmdLine::CmdLine(const std::string& m,
_message(m), _message(m),
_version(v), _version(v),
_numRequired(0), _numRequired(0),
_delimiter(delim) _delimiter(delim),
_userSetOutput(false)
{ {
_constructor(); _constructor();
} }
@ -289,6 +297,9 @@ inline CmdLine::~CmdLine()
visIter != _visitorDeleteOnExitList.end(); visIter != _visitorDeleteOnExitList.end();
++visIter) ++visIter)
delete *visIter; delete *visIter;
if ( !_userSetOutput )
delete _output;
} }
inline void CmdLine::_constructor() inline void CmdLine::_constructor()
@ -439,6 +450,7 @@ inline CmdLineOutput* CmdLine::getOutput()
inline void CmdLine::setOutput(CmdLineOutput* co) inline void CmdLine::setOutput(CmdLineOutput* co)
{ {
_userSetOutput = true;
_output = co; _output = co;
} }