COIN-OR::LEMON - Graph Library

Ticket #17: 7c796c1cf1b0.patch

File 7c796c1cf1b0.patch, 5.6 KB (added by Balazs Dezso, 16 years ago)

Bug fix in exception throwing

  • lemon/lgf_reader.h

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1223233741 -7200
    # Node ID 7c796c1cf1b094ce098b8a02a85336425c22c970
    # Parent  cbe3ec2d59d2d7c539ab62f2ca6796c960795237
    Fix memory leak hazard
    
    If the constructor throws an exception, it should deallocate each
    dynamically allocated memory.
    
    diff -r cbe3ec2d59d2 -r 7c796c1cf1b0 lemon/lgf_reader.h
    a b  
    517517        _filename(fn), _digraph(digraph),
    518518        _use_nodes(false), _use_arcs(false),
    519519        _skip_nodes(false), _skip_arcs(false) {
    520       if (!(*_is)) throw IoError("Cannot open file", fn);
     520      if (!(*_is)) {
     521        delete _is;
     522        throw IoError("Cannot open file", fn);
     523      }
    521524    }
    522525
    523526    /// \brief Constructor
     
    529532        _filename(fn), _digraph(digraph),
    530533        _use_nodes(false), _use_arcs(false),
    531534        _skip_nodes(false), _skip_arcs(false) {
    532       if (!(*_is)) throw IoError("Cannot open file", fn);
     535      if (!(*_is)) {
     536        delete _is;
     537        throw IoError("Cannot open file", fn);
     538      }
    533539    }
    534540
    535541    /// \brief Destructor
     
    13081314        _filename(fn), _graph(graph),
    13091315        _use_nodes(false), _use_edges(false),
    13101316        _skip_nodes(false), _skip_edges(false) {
    1311       if (!(*_is)) throw IoError("Cannot open file", fn);
     1317      if (!(*_is)) {
     1318        delete _is;
     1319        throw IoError("Cannot open file", fn);
     1320      }
    13121321    }
    13131322
    13141323    /// \brief Constructor
     
    13201329        _filename(fn), _graph(graph),
    13211330        _use_nodes(false), _use_edges(false),
    13221331        _skip_nodes(false), _skip_edges(false) {
    1323       if (!(*_is)) throw IoError("Cannot open file", fn);
     1332      if (!(*_is)) {
     1333        delete _is;
     1334        throw IoError("Cannot open file", fn);
     1335      }
    13241336    }
    13251337
    13261338    /// \brief Destructor
     
    20942106    SectionReader(const std::string& fn)
    20952107      : _is(new std::ifstream(fn.c_str())), local_is(true),
    20962108        _filename(fn) {
    2097       if (!(*_is)) throw IoError("Cannot open file", fn);
     2109      if (!(*_is)) {
     2110        delete _is;
     2111        throw IoError("Cannot open file", fn);
     2112      }
    20982113    }
    20992114
    21002115    /// \brief Constructor
     
    21032118    SectionReader(const char* fn)
    21042119      : _is(new std::ifstream(fn)), local_is(true),
    21052120        _filename(fn) {
    2106       if (!(*_is)) throw IoError("Cannot open file", fn);
     2121      if (!(*_is)) {
     2122        delete _is;
     2123        throw IoError("Cannot open file", fn);
     2124      }
    21072125    }
    21082126
    21092127    /// \brief Destructor
     
    23862404    /// file.
    23872405    LgfContents(const std::string& fn)
    23882406      : _is(new std::ifstream(fn.c_str())), local_is(true) {
    2389       if (!(*_is)) throw IoError("Cannot open file", fn);
     2407      if (!(*_is)) {
     2408        delete _is;
     2409        throw IoError("Cannot open file", fn);
     2410      }
    23902411    }
    23912412
    23922413    /// \brief Constructor
     
    23952416    /// file.
    23962417    LgfContents(const char* fn)
    23972418      : _is(new std::ifstream(fn)), local_is(true) {
    2398       if (!(*_is)) throw IoError("Cannot open file", fn);
     2419      if (!(*_is)) {
     2420        delete _is;
     2421        throw IoError("Cannot open file", fn);
     2422      }
    23992423    }
    24002424
    24012425    /// \brief Destructor
  • lemon/lgf_writer.h

    diff -r cbe3ec2d59d2 -r 7c796c1cf1b0 lemon/lgf_writer.h
    a b  
    463463    DigraphWriter(const Digraph& digraph, const std::string& fn)
    464464      : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
    465465        _skip_nodes(false), _skip_arcs(false) {
    466       if (!(*_os)) throw IoError("Cannot write file", fn);
     466      if (!(*_os)) {
     467        delete _os;
     468        throw IoError("Cannot write file", fn);
     469      }
    467470    }
    468471
    469472    /// \brief Constructor
     
    473476    DigraphWriter(const Digraph& digraph, const char* fn)
    474477      : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
    475478        _skip_nodes(false), _skip_arcs(false) {
    476       if (!(*_os)) throw IoError("Cannot write file", fn);
     479      if (!(*_os)) {
     480        delete _os;
     481        throw IoError("Cannot write file", fn);
     482      }
    477483    }
    478484
    479485    /// \brief Destructor
     
    10241030    GraphWriter(const Graph& graph, const std::string& fn)
    10251031      : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
    10261032        _skip_nodes(false), _skip_edges(false) {
    1027       if (!(*_os)) throw IoError("Cannot write file", fn);
     1033      if (!(*_os)) {
     1034        delete _os;
     1035        throw IoError("Cannot write file", fn);
     1036      }
    10281037    }
    10291038
    10301039    /// \brief Constructor
     
    10341043    GraphWriter(const Graph& graph, const char* fn)
    10351044      : _os(new std::ofstream(fn)), local_os(true), _graph(graph),
    10361045        _skip_nodes(false), _skip_edges(false) {
    1037       if (!(*_os)) throw IoError("Cannot write file", fn);
     1046      if (!(*_os)) {
     1047        delete _os;
     1048        throw IoError("Cannot write file", fn);
     1049      }
    10381050    }
    10391051
    10401052    /// \brief Destructor
     
    15871599    /// Construct a section writer, which writes into the given file.
    15881600    SectionWriter(const std::string& fn)
    15891601      : _os(new std::ofstream(fn.c_str())), local_os(true) {
    1590       if (!(*_os)) throw IoError("Cannot write file", fn);
     1602      if (!(*_os)) {
     1603        delete _os;
     1604        throw IoError("Cannot write file", fn);
     1605      }
    15911606    }
    15921607
    15931608    /// \brief Constructor
     
    15951610    /// Construct a section writer, which writes into the given file.
    15961611    SectionWriter(const char* fn)
    15971612      : _os(new std::ofstream(fn)), local_os(true) {
    1598       if (!(*_os)) throw IoError("Cannot write file", fn);
     1613      if (!(*_os)) {
     1614        delete _os;
     1615        throw IoError("Cannot write file", fn);
     1616      }
    15991617    }
    16001618
    16011619    /// \brief Destructor