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

                 Key: STDCXX-268
                 URL: http://issues.apache.org/jira/browse/STDCXX-268
             Project: C++ Standard Library
          Issue Type: Bug
          Components: 23. Containers
    Affects Versions: 4.1.3
         Environment: All
            Reporter: Farid Zaripov


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/[EMAIL
 PROTECTED]

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.


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to