Crash when using dynamic_cast in GCC!
Posted by Steve Hanna on July 8th, 2008 filed in Random Tags: "-fno-exceptions", c++, coding, compiler, crash, dynamic_cast, flags, gcc, research, segfaultMost of the time, I don’t post about these sorts of things on my blog; however, because the corpus of the internet didn’t offer a clear solution, I decided to edify the world with my hours wasted.
Working with a large code base, it’s easy to just make the required modifications and keep moving.
So you probably have some code like this:
class A { };
class B : public A { };
//imagine a situation when you actually need dynamic cast,
//it’s difficult. if you use it all the time, you need to reevaluate
//your class hierarchy and coding style!
B b;
A* b_a = dynamic_cast<A*>(&b);
if (b_a) { /* yep, it worked */ }
If you have the flag -fno-rtti enabled, it WILL segmentation fault! It will not even throw a warning. Do not pass Go, do not collect $200. Solution: turn on run time type information by removing that flag!’
I find this behavior to be quite unbelievable, especially because if you disable exceptions (-fno-exceptions), GCC will not compile the code and complain about the flag being enabled; why isn’t this the behavior for RTTI?








Leave a Comment