I ran into a problem using dynamic_cast for objects created in a loaded shared library, but only if the class contains a method that overrides another method.
I am using Xcode 4.3 with the Apple LLVM 3.1 compiler. I compiled the same code with gcc and clang on linux and have no problem, so I assume this is a compiler error in Xcode, but has anyone seen this before?
Suppose the class definitions in the heading are "test3.h"
#pragma once class c1 { public: virtual ~c1 (); virtual void foo (); }; class c2 : public c1 { public: void foo () override; }; class c3 : public c1 { public: };
Suppose the implementation code in a static library in the source file is called test3.cpp
#include "test3.h" c1::~c1 () { } void c1::foo () { } void c2::foo () { }
Suppose a simple simple library in the source file test2.cpp
#include "test3.h" extern "C" c1 * get1 () { return new c2; } extern "C" c1 * get2 () { return new c3; }
Suppose a simple executable application in the source file test1.cpp
#include "test3.h" #include <dlfcn.h> #include <iostream> int main () { auto lib (dlopen ("libtest2.dylib", RTLD_NOW | RTLD_GLOBAL)); auto a1 (dlsym (lib, "get1")); auto a2 (dlsym (lib, "get2")); auto f1 ((c1 * (*) ())a1); auto f2 ((c1 * (*) ())a2); auto o1 (f1 ()); auto o2 (f2 ()); auto d1 (dynamic_cast <c2 *> (o1)); auto d2 (dynamic_cast <c3 *> (o2)); auto result1 (d1 != 0); auto result2 (d2 != 0); std::cout << result1 << std::endl; std::cout << result2 << std::endl; }
When the test program is running, result1 is false, and result2 is true. I expect both results1 and result2 to be true.
Has anyone seen this or thought of a workaround?
source share