Topic: new/ctor throw/delete


Author: "John E. Potter" <jpotter@falcon.lhup.edu>
Date: 1997/12/02
Raw View
Prior to FDIS, there was discussion that the wording did not assure that
the proper (or any) operator delete would be invoked when a ctor throws
during a new expression.  Does the wording now assure this?  Simple
example follows with expected output in the cases of no throw and
throw.  I assume the answer would also apply to operator new/delete[].

John

int point;
struct Thrower {
    Thrower () : me(++ count) {
        cout << me << " born\n";
        if (me == point)
            throw me;
        }
    ~Thrower () {
        cout << me << " dead\n";
        }
    void* operator new (size_t s) {
        cout << "operator new\n";
        return ::operator new(s);
        }
    void operator delete (void* p) {
        cout << "operator delete\n";
        ::operator delete(p);
        }
    static int count;
    int me;
    };
int Thrower::count(0);
int main (int argc, char**) {
    point = argc - 1;
    try {
        Thrower* q(new Thrower);
        delete q;
        }
        catch (int w) {
            cout << "caught " << w << '\n';
            }
    }

operator new
1 born
1 dead
operator delete

operator new
1 born
operator delete
caught 1
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: stephen.clamage_nospam@eng.sun.com (Steve Clamage)
Date: 1997/12/03
Raw View
On 02 Dec 97 08:55:12 GMT, "John E. Potter" <jpotter@falcon.lhup.edu>
wrote:

>Prior to FDIS, there was discussion that the wording did not assure that
>the proper (or any) operator delete would be invoked when a ctor throws
>during a new expression.  Does the wording now assure this?

For quite some time the draft has addressed the issue precisely. I
don't think the FDIS differs from CD2, the public-comment version that
was posted a year ago. See section 15.2 "Constructors and
Destructors".

---
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]