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

Bug#188943: g++-3.2: code using dynamic_cast causes segfaults when -fno-rtti is used



On Thursday 01 May 2003 06:57, Matthias Klose wrote:
> Please can you check, if the behaviour is the same with g++-3.3?
>

Yes, it is.  Attached is a program which shows this problem.  Normally it will 
work just fine however add -fno-rtti and it segfaults. Using -Wall -W 
-pedantic does not give any hints that -fno-rtti is a bad if dynamic_cast() 
is used.

No special options need to be passed to build this program.

I used 3.3.0-pre7 for this test.  Hope the test program helps out.  It was the 
smallest version I could come up with.  This shows approximately what the C++ 
project that insprired this bug was doing.

#include <iostream>

class EventHandler {
public:
  virtual ~EventHandler(void) {}

  virtual void thisEvent(void) {}
  virtual void thatEvent(void) {}
};

class MyMenu: public EventHandler {
public:
  virtual void thisEvent(void) { std::cout << "this Menu handler\n"; }
  virtual void thatEvent(void) { std::cout << "that Menu handler\n"; }
};

class OtherThing: public EventHandler {
public:
  virtual void thisEvent(void) { std::cout << "this OtherThing handler\n"; }
  virtual void thatEvent(void) { std::cout << "that OtherThing handler\n"; }
};

void do_event(EventHandler* handler) {
  if (dynamic_cast<MyMenu*>(handler)) {
    // also call thatHandler for menus
    handler->thatEvent();
  }

  handler->thisEvent();
}

int main(int argc, char* argv[]) {
  MyMenu menu;
  OtherThing thing;

  do_event(&menu);

  do_event(&thing);

  exit(0);
}

Reply to: