Uploaded image for project: 'C++ Standard Library'
  1. C++ Standard Library
  2. STDCXX-268

std::list constructors do not call destructors for created objects if exception was thrown

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.1.3
    • 4.2.0
    • 23. Containers
    • None
    • All

    Description

      std::list constructors do not satisfy basic exception safety requirement (no memory leaks) since they do not call destructors for created objects if exception was thrown.

      See details here:
      http://mail-archives.apache.org/mod_mbox/incubator-stdcxx-dev/200608.mbox/%3cF92433E3D38672499C549EB615AFADA0714819@exkiv.kyiv.vdiweb.com%3e

      test.cpp:
      -----------------------
      #include <list>
      #include <cassert>

      static int throw_inx = -1;

      class ListItem
      {
      public:
      static int count_;

      void test ()

      { if (throw_inx == count_) throw count_; ++count_; }

      ListItem ()

      { test (); }

      ListItem (const ListItem&) { test (); }

      ~ListItem ()

      { --count_; }

      };

      int ListItem::count_ = 0;

      int main(int argc, char* argv[])
      {
      typedef std::list<ListItem> List;
      ListItem items [20];
      List lst (20);

      bool thrown = false;
      throw_inx = 10;

      try

      { ListItem::count_ = 0; List test (20); }

      catch (...)

      { thrown = true; }

      assert (thrown);
      assert (0 == ListItem::count_);

      try { thrown = false; ListItem::count_ = 0; List test (20, items [0]); } catch (...) { thrown = true; }

      assert (thrown);
      assert (0 == ListItem::count_);

      try

      { thrown = false; ListItem::count_ = 0; List test (items, items + 20); }

      catch (...)

      { thrown = true; }

      assert (thrown);
      assert (0 == ListItem::count_);

      try { thrown = false; ListItem::count_ = 0; List test (lst.begin (), lst.end ()); } catch (...) { thrown = true; }

      assert (thrown);
      assert (0 == ListItem::count_);

      try

      { thrown = false; ListItem::count_ = 0; List test (lst); }

      catch (...)

      { thrown = true; }

      assert (thrown);
      assert (0 == ListItem::count_);

      return 0;
      }

      the test output:
      ---------------------
      test: /usr/src/tests/test.cpp:45: int main(int, char**): Assertion `0 == ListItem::count_' failed.
      Aborted.

      Attachments

        Activity

          People

            farid Farid Zaripov
            farid Farid Zaripov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: