The code below fails during the 7th loop iteration with an exception
'bad_cast':


#include <iostream>
using namespace std;

class A
{
public:
  virtual ~A()
  {}
};

class B : 
  public A
{
};

int main()
{
  string arg = "something";

  int n = 0;
  while (true)
    {
      cout << "n=" << ++n << endl;
      A* a = new B;
      B& ret = dynamic_cast<B&>(*a);
    }
}



The output is the following:
n=1
n=2
n=3
n=4
n=5
n=6
n=7
terminate called after throwing an instance of 'std::bad_cast'
  what():  St8bad_cast
Aborted


It fails on x86_64-unknown-linux-gnu, gcc version 4.2.0 20060513.
It does work on i686-pc-linux-gnu and seems to work on earlier versions of the
4.2.0 branch.
It also works correctly when the seemingly unrelated line

  string arg = "something";

is removed.


-- 
           Summary: dynamic cast failure
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ingo dot josopait at gmx dot de
  GCC host triplet: x86_64-unknown-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27592

Reply via email to