code using dynamic_cast causes segfaults when -fno-rtti is used
>Submitter-Id: net
>Originator: Sean 'Shaleh' Perry <shalehperry@attbi.com>
>Organization: The Debian Project
>Confidential: no
>Synopsis:
>Severity: serious
>Priority: medium
>Category: c++
>Class: sw-bug
>Release: 3.3 (Debian) (Debian testing/unstable)
>Environment:
System: Debian GNU/Linux (unstable)
Architecture: i686
>Description:
[ Reported to the Debian BTS as report #188943.
Please CC 188943@bugs.debian.org on replies.
Log of report can be found at http://bugs.debian.org/188943 ]
3.2.3 / 3.3
Now obviously, dynamic_cast requires rtti so using -fno-rtti is silly.
I ran into this on a C++ project where we had been using -fno-rtti because rtti
was not used. A recent commit added dynamic_cast code and started segfaulting.
It was believed that the dynamic_cast was at fault and this caused some finger
pointing. Then the no-rtti was noticed.
The compiler should give a warning (or perhaps even an error) if code requiring
rtti is used while the -fno-rtti option is in effect.
#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);
}
>How-To-Repeat:
>Fix:
Reply to: