Bug: static object inside a function

eyal.ben-david@aks.com eyal.ben-david@aks.com
Tue Oct 6 16:19:00 GMT 1998


This problem was reported in Windows Developer Journal.
Originally the bug reported for MSVC but EGCS 1.1 (mingw32 version)
has this bug too.

consider the following function:

XX* f()
{
   static XX x;
   return &xx;
}

suppose the constructor of XX throws. then in the next call to f() the XX
object
should be constructed once again (there is no skip over the static init)
This is not the case in EGCS 1.1 - the object is marked as constructed
even if the first initialization throws.

Here is a small program that demonstrates the bug:


#include <iostream>

struct X
{
   X() { throw 1; }
};

X& getX()
{
   static X x;
   return x;
}

int main()
{
   using namespace std;

   try {
      getX();
      cout << "*** BUG ***\n";
   }
   catch(...) {
      cout << "OK\n";
   }

   try {
      getX();
      cout << "*** BUG ***\n";
   }
   catch(...) {
      cout << "OK\n";
   }
}


Eyal.





More information about the Gcc mailing list