bug-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

multiple inheritence of exceptions


From: Thomas Langen
Subject: multiple inheritence of exceptions
Date: Sat, 14 Apr 2001 17:40:09 +0200

For an exception derived from two base classes and caught as one of
those base classes it depends of their declaration order whether they
are deconstructed correctly.

The attached code for g++ 2.95.3 shows that the addresses of the
exception get mixed up when REORDER is not defined:

Output for REORDER defined:

constructing A at 0xbffff634
constructing B at 0xbffff638
constructing C at 0xbffff634
constructing A at 0x804b698
constructing B at 0x804b69c
copy constructing C at 0x804b698
deconstructing C at 0xbffff634
deconstructing B at 0xbffff638
deconstructing A at 0xbffff634
A:  at 0x804b698
deconstructing C at 0x804b698
deconstructing B at 0x804b69c
deconstructing A at 0x804b698


Output for REORDER undefined:

constructing B at 0xbffff634
constructing A at 0xbffff638
constructing C at 0xbffff634
constructing B at 0x804b698
constructing A at 0x804b69c
copy constructing C at 0x804b698
deconstructing C at 0xbffff634
deconstructing A at 0xbffff638
deconstructing B at 0xbffff634
A:  at 0x804b69c
deconstructing C at 0x804b69c               ! should be 0x804b698
deconstructing A at 0x804b6a0               ! should be 0x804b69c
deconstructing B at 0x804b69c               ! should be 0x804b698

For more complex exceptions this ends up in a segmentation fault.

-- 
Thomas Langen
address@hidden
#include "/home/langen/MySQLaux/Error.h"

class A
{
protected:
  A()
  {
    cout << "constructing A at " << &*this  << endl;
  }
  virtual ~A() 
  {
    cout << "deconstructing A at " << &*this << endl;
  }
};

class B
{
protected:
  B()
  {
    cout << "constructing B at " << &*this<< endl;
  }
  virtual ~B() 
  {
    cout << "deconstructing B at " << &*this << endl;
  }
};

#ifdef REORDER 
class C: public A, public B
#else
class C: public B, public A
#endif
{
public:
  C()
  {
    cout << "constructing C at " << &*this<< endl;
  }
  C(const C & ex)
  {
    cout << "copy constructing C at " << &*this<< endl;
  }
  virtual ~C() 
  {
    cout << "deconstructing C at " << &*this << endl;
  }
};

int main()
{
  try
  {
    throw C();
  }
  catch (A const & ex)
  {
    cout << "A: " << " at " << &ex << endl;
  }  

  return 0;
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]