From 3c4771b03c5888a082332766ae4bdd52e4ff7b39 Mon Sep 17 00:00:00 2001 From: mes5k Date: Tue, 4 Jan 2005 20:21:20 +0000 Subject: [PATCH] fixed output memory leak --- docs/manual.html | 5 ++++- docs/manual.xml | 5 ++++- include/tclap/CmdLine.h | 16 ++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/manual.html b/docs/manual.html index fe3bc1e..0da48ce 100644 --- a/docs/manual.html +++ b/docs/manual.html @@ -591,7 +591,10 @@ int main(int argc, char** argv)

See test4.cpp in the examples directory for the full -example. +example. NOTE: if you supply your own Output object, we +will not delete it in the CmdLine destructor. This +could lead to a (very small) memory leak if you don't take care of the object +yourself.

Chapter 3. Exceptions to the Rules

Like all good rules, there are many exceptions....

Ignoring arguments

diff --git a/docs/manual.xml b/docs/manual.xml index 9592f31..5651813 100644 --- a/docs/manual.xml +++ b/docs/manual.xml @@ -750,7 +750,10 @@ int main(int argc, char** argv) See test4.cpp in the examples directory for the full -example. +example. NOTE: if you supply your own Output object, we +will not delete it in the CmdLine destructor. This +could lead to a (very small) memory leak if you don't take care of the object +yourself. diff --git a/include/tclap/CmdLine.h b/include/tclap/CmdLine.h index 84e0898..67c2c61 100644 --- a/include/tclap/CmdLine.h +++ b/include/tclap/CmdLine.h @@ -136,6 +136,12 @@ class CmdLine : public CmdLineInterface */ 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: /** @@ -258,7 +264,8 @@ inline CmdLine::CmdLine(const std::string& n, _message(m), _version(v), _numRequired(0), - _delimiter(' ') + _delimiter(' '), + _userSetOutput(false) { _constructor(); } @@ -270,7 +277,8 @@ inline CmdLine::CmdLine(const std::string& m, _message(m), _version(v), _numRequired(0), - _delimiter(delim) + _delimiter(delim), + _userSetOutput(false) { _constructor(); } @@ -289,6 +297,9 @@ inline CmdLine::~CmdLine() visIter != _visitorDeleteOnExitList.end(); ++visIter) delete *visIter; + + if ( !_userSetOutput ) + delete _output; } inline void CmdLine::_constructor() @@ -439,6 +450,7 @@ inline CmdLineOutput* CmdLine::getOutput() inline void CmdLine::setOutput(CmdLineOutput* co) { + _userSetOutput = true; _output = co; }