Object destructed twice?

This is a discussion on Object destructed twice? within the C++ Programming forums, part of the General Programming Boards category; If I compile this Code: #include <iostream> #include <exception> using namespace std; class X { public: static int i; int ...

  1. #1
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    Object destructed twice?

    If I compile this

    Code:
    #include <iostream>
    #include <exception>
    using namespace std;
    
    class X
    {
        public:
        static int i;
        int count;
        X(){count = ++i; cout << "constructing X object " << count << endl;}
        ~X(){cout << "destructing X object " << count << endl;}
    };
    
    int X::i = 0;
    
    void f() throw (X)
    {
        throw 1;
    }
    
    void g()
    {
        cout << "unexpected handler g() called\n";
        throw X();
    }
    
    int main()
    {
        set_unexpected(&g);
        try
        {
            f();
        }
        catch (X)
        {
            cout << "caught exception X\n";
        }
        catch (int)
        {
    
            cout << "caught exception int\n";
        }
    
        return 0;
    }
    The X object thrown by g() appears to get destructed twice. Is this right ? Or is there something very wrong with the program? Or is my compiler sick?

    Any ideas?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Try implementing the copy constructor to match what your default constructor does

    EDIT:
    By the way, you should have caught the X object by const reference instead of by value, though if you did you probably would not have observed the "problem" that you described.
    Last edited by laserlight; 01-31-2009 at 12:57 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,831
    You should always catch by reference.

    Soma

    Edit: Eh...

  4. #4
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    So obvious when it's pointed out.

    Thanks a million.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, see here why you should avoid using Exception Specifications.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Quote Originally Posted by phantomotap View Post
    You should always catch by reference.
    Yes, and const-reference when possible.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,831
    Yes, and const-reference when possible.
    True. A catch for a non-constant reference is only useful when you need to patch a polymorphic exception, and if you need to do that you've probably failed anyway.

    Soma

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Catch by non-const is also useful if you want to supply the exception with additional information and then rethrow. Boost.Exception provides an exception class that you can attach arbitrary information to.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21