This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Compiler bug throwing the result of a function call.


/* exception.cc

  This file illustrates a mismatch between constructors, copy
  constructors and destructors for an object that is thrown in an
  exception:

  % uname -a
  Linux foobar 2.0.36 #1 Tue Oct 13 22:17:11 EDT 1998 i686 unknown
  % g++ -v
  Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.66/specs
  gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)
  % g++ -o exception exception.cc
  % ./exception
  Construct 0xefffed68.
  Copy      0x37330 <= 0xefffed70.
  Destruct  0xefffed70.
  Destruct  0xefffed68.
  Destruct  0x37330.
  %

  Notes:
  1. The copy constructor is being passed a non-existent object.
  2. Remove the '.x()' in the thrown expression and it works.
  3. Modify X::x() to return X instead of X& and it works.
  4. This also fails on a SPARC Solaris 2.5.1 with egcs 1.1.1.

 */

#include <iostream>
#include <exception>

class X {
public:
    X& x()        { return *this; }
    X()           { cerr << "Construct " << this << ".\n"; }
    X(const X& x) { cerr << "Copy      " << this << " <= " << &x << ".\n"; }
    X& operator=(const X& x)
                  { cerr << "Assign    " << this << " <= " << &x << ".\n"; }
    ~X()          { cerr << "Destruct  " << this << ".\n"; }
};

int main ()
{
    try
    {
        throw X().x();
    }
    catch(...) { }
}


-- 
http://www.simdb.com/~marcelo/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]