Fix memory leak hazard
authorBalazs Dezso <deba@inf.elte.hu>
Sun, 05 Oct 2008 21:09:01 +0200
changeset 2957c796c1cf1b0
parent 294 cbe3ec2d59d2
child 297 92b193385702
Fix memory leak hazard

If the constructor throws an exception, it should deallocate each
dynamically allocated memory.
lemon/lgf_reader.h
lemon/lgf_writer.h
     1.1 --- a/lemon/lgf_reader.h	Wed Oct 01 13:56:40 2008 +0200
     1.2 +++ b/lemon/lgf_reader.h	Sun Oct 05 21:09:01 2008 +0200
     1.3 @@ -517,7 +517,10 @@
     1.4          _filename(fn), _digraph(digraph),
     1.5          _use_nodes(false), _use_arcs(false),
     1.6          _skip_nodes(false), _skip_arcs(false) {
     1.7 -      if (!(*_is)) throw IoError("Cannot open file", fn);
     1.8 +      if (!(*_is)) {
     1.9 +        delete _is;
    1.10 +        throw IoError("Cannot open file", fn);
    1.11 +      }
    1.12      }
    1.13  
    1.14      /// \brief Constructor
    1.15 @@ -529,7 +532,10 @@
    1.16          _filename(fn), _digraph(digraph),
    1.17          _use_nodes(false), _use_arcs(false),
    1.18          _skip_nodes(false), _skip_arcs(false) {
    1.19 -      if (!(*_is)) throw IoError("Cannot open file", fn);
    1.20 +      if (!(*_is)) {
    1.21 +        delete _is;
    1.22 +        throw IoError("Cannot open file", fn);
    1.23 +      }
    1.24      }
    1.25  
    1.26      /// \brief Destructor
    1.27 @@ -1308,7 +1314,10 @@
    1.28          _filename(fn), _graph(graph),
    1.29          _use_nodes(false), _use_edges(false),
    1.30          _skip_nodes(false), _skip_edges(false) {
    1.31 -      if (!(*_is)) throw IoError("Cannot open file", fn);
    1.32 +      if (!(*_is)) {
    1.33 +        delete _is;
    1.34 +        throw IoError("Cannot open file", fn);
    1.35 +      }
    1.36      }
    1.37  
    1.38      /// \brief Constructor
    1.39 @@ -1320,7 +1329,10 @@
    1.40          _filename(fn), _graph(graph),
    1.41          _use_nodes(false), _use_edges(false),
    1.42          _skip_nodes(false), _skip_edges(false) {
    1.43 -      if (!(*_is)) throw IoError("Cannot open file", fn);
    1.44 +      if (!(*_is)) {
    1.45 +        delete _is;
    1.46 +        throw IoError("Cannot open file", fn);
    1.47 +      }
    1.48      }
    1.49  
    1.50      /// \brief Destructor
    1.51 @@ -2094,7 +2106,10 @@
    1.52      SectionReader(const std::string& fn)
    1.53        : _is(new std::ifstream(fn.c_str())), local_is(true),
    1.54          _filename(fn) {
    1.55 -      if (!(*_is)) throw IoError("Cannot open file", fn);
    1.56 +      if (!(*_is)) {
    1.57 +        delete _is;
    1.58 +        throw IoError("Cannot open file", fn);
    1.59 +      }
    1.60      }
    1.61  
    1.62      /// \brief Constructor
    1.63 @@ -2103,7 +2118,10 @@
    1.64      SectionReader(const char* fn)
    1.65        : _is(new std::ifstream(fn)), local_is(true),
    1.66          _filename(fn) {
    1.67 -      if (!(*_is)) throw IoError("Cannot open file", fn);
    1.68 +      if (!(*_is)) {
    1.69 +        delete _is;
    1.70 +        throw IoError("Cannot open file", fn);
    1.71 +      }
    1.72      }
    1.73  
    1.74      /// \brief Destructor
    1.75 @@ -2386,7 +2404,10 @@
    1.76      /// file.
    1.77      LgfContents(const std::string& fn)
    1.78        : _is(new std::ifstream(fn.c_str())), local_is(true) {
    1.79 -      if (!(*_is)) throw IoError("Cannot open file", fn);
    1.80 +      if (!(*_is)) {
    1.81 +        delete _is;
    1.82 +        throw IoError("Cannot open file", fn);
    1.83 +      }
    1.84      }
    1.85  
    1.86      /// \brief Constructor
    1.87 @@ -2395,7 +2416,10 @@
    1.88      /// file.
    1.89      LgfContents(const char* fn)
    1.90        : _is(new std::ifstream(fn)), local_is(true) {
    1.91 -      if (!(*_is)) throw IoError("Cannot open file", fn);
    1.92 +      if (!(*_is)) {
    1.93 +        delete _is;
    1.94 +        throw IoError("Cannot open file", fn);
    1.95 +      }
    1.96      }
    1.97  
    1.98      /// \brief Destructor
     2.1 --- a/lemon/lgf_writer.h	Wed Oct 01 13:56:40 2008 +0200
     2.2 +++ b/lemon/lgf_writer.h	Sun Oct 05 21:09:01 2008 +0200
     2.3 @@ -463,7 +463,10 @@
     2.4      DigraphWriter(const Digraph& digraph, const std::string& fn)
     2.5        : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
     2.6          _skip_nodes(false), _skip_arcs(false) {
     2.7 -      if (!(*_os)) throw IoError("Cannot write file", fn);
     2.8 +      if (!(*_os)) {
     2.9 +        delete _os;
    2.10 +        throw IoError("Cannot write file", fn);
    2.11 +      }
    2.12      }
    2.13  
    2.14      /// \brief Constructor
    2.15 @@ -473,7 +476,10 @@
    2.16      DigraphWriter(const Digraph& digraph, const char* fn)
    2.17        : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
    2.18          _skip_nodes(false), _skip_arcs(false) {
    2.19 -      if (!(*_os)) throw IoError("Cannot write file", fn);
    2.20 +      if (!(*_os)) {
    2.21 +        delete _os;
    2.22 +        throw IoError("Cannot write file", fn);
    2.23 +      }
    2.24      }
    2.25  
    2.26      /// \brief Destructor
    2.27 @@ -1024,7 +1030,10 @@
    2.28      GraphWriter(const Graph& graph, const std::string& fn)
    2.29        : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
    2.30          _skip_nodes(false), _skip_edges(false) {
    2.31 -      if (!(*_os)) throw IoError("Cannot write file", fn);
    2.32 +      if (!(*_os)) {
    2.33 +        delete _os;
    2.34 +        throw IoError("Cannot write file", fn);
    2.35 +      }
    2.36      }
    2.37  
    2.38      /// \brief Constructor
    2.39 @@ -1034,7 +1043,10 @@
    2.40      GraphWriter(const Graph& graph, const char* fn)
    2.41        : _os(new std::ofstream(fn)), local_os(true), _graph(graph),
    2.42          _skip_nodes(false), _skip_edges(false) {
    2.43 -      if (!(*_os)) throw IoError("Cannot write file", fn);
    2.44 +      if (!(*_os)) {
    2.45 +        delete _os;
    2.46 +        throw IoError("Cannot write file", fn);
    2.47 +      }
    2.48      }
    2.49  
    2.50      /// \brief Destructor
    2.51 @@ -1587,7 +1599,10 @@
    2.52      /// Construct a section writer, which writes into the given file.
    2.53      SectionWriter(const std::string& fn)
    2.54        : _os(new std::ofstream(fn.c_str())), local_os(true) {
    2.55 -      if (!(*_os)) throw IoError("Cannot write file", fn);
    2.56 +      if (!(*_os)) {
    2.57 +        delete _os;
    2.58 +        throw IoError("Cannot write file", fn);
    2.59 +      }
    2.60      }
    2.61  
    2.62      /// \brief Constructor
    2.63 @@ -1595,7 +1610,10 @@
    2.64      /// Construct a section writer, which writes into the given file.
    2.65      SectionWriter(const char* fn)
    2.66        : _os(new std::ofstream(fn)), local_os(true) {
    2.67 -      if (!(*_os)) throw IoError("Cannot write file", fn);
    2.68 +      if (!(*_os)) {
    2.69 +        delete _os;
    2.70 +        throw IoError("Cannot write file", fn);
    2.71 +      }
    2.72      }
    2.73  
    2.74      /// \brief Destructor