"Boris" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > [crossposted to gnu.g++.help] > > Boris wrote: >> Boris wrote: >>> [...] The code I talk about looks like this: >>> >>> if (typeid(b) == typeid(level2)) >>> { >>> const level1 *l1 = dynamic_cast<const level1*>(&b); >>> } >> >> I changed the code for testing purposes and replaced dynamic_cast with >> reinterpret_cast - this works. >> >> I changed then the code to make it look like this: >> >> if (typeid(b) == typeid(level2)) >> { >> const level2 *l2 = dynamic_cast<const level2*>(&b); >> const level1 *l1 = l2; >> } >> >> This works, too. Is there any other possible explanation than a >> compiler bug why a dynamic_cast<const level1*> should not work? > > It looks like a problem with g++. The code I was talking about is in a > shared library. When I link the executable statically dynamic_cast works. > When I use however the shared library dynamic_cast returns 0. Same code but > different behavior due to linking. > > There is a section "dynamic_cast, throw, typeid don't work with shared > libraries" at http://gcc.gnu.org/faq.html#dso. From what I understand though > there is not much you can do? I'm using version 3.4.6 of g++ which is the > latest of the 3.4.x series. > > The code construct as above with dynamic_cast is used in various project > files. When building and linking the shared library some work, others don't. > I don't know though on what it depends why some dynamic_casts don't work and > falsely return 0. Is there anything I can do except replacing all > dynamic_casts? Any g++ experts with ideas? > > Boris
Interesting. A few years ago I had a similar problem on an OpenVMS system. The dynamic_cast did not work properly in a function that was called from another function in a shareable image (which is similar to a dynamic library under Linux). An example was code running in a separate thread. Since the pthread library was in a shareable image, functions running in another thread were called from functions in a shareable image. The OpenVMS engineers brought out a patch for the compiler and the run-time library quickly. The similarity of these problems strikes me, as the compiler environments are from very different teams. Apparently there is something in dynamic_cast that makes it difficult to work from pre-linked parts of a program. Fred.Zwarts. _______________________________________________ help-gplusplus mailing list help-gplusplus@gnu.org http://lists.gnu.org/mailman/listinfo/help-gplusplus