I am seeing a very strange failure that is very similar to the problem described in
dynamic_cast on llvm clang compiler failing: dynamic_cast
returns nullptr
when I am trying to downcast from base type to derived type, for an object of derived type. In my case though typeid
actually shows the same dynamic type.
Below is the debug function that I use to verify run-time types. It takes a pointer of SrcType*
and tries to dynamically cast it to pointer of DstType*
. If conversion fails, i.e. the returned pointer is nullptr
, it prints an error message. VERIFY()
macro checks if condition is true:
template<typename DstType, typename SrcType>
void CheckDynamicType( SrcType *pSrcPtr )
{
VERIFY(dynamic_cast<DstType*> (pSrcPtr) != nullptr,
"Dynamic type cast failed. Src typeid: \'", typeid(*pSrcPtr).name(),
"\' Dst typeid: \'", typeid(DstType).name(), '\'');
}
And here is the output that I am getting:
Dynamic type cast failed. Src typeid: 'N8Diligent15RefCountersImplE' Dst typeid: 'N8Diligent15RefCountersImplE'
I am using clang compiler from Android ndk r16 toolchain. I am compiling with rtti flag. The same code works fine on gcc and msvc.
Another bit of information: the code also works fine if I build it with gnustl
runtime (I am not sure how gnu stl works with clang, but anyway).
UPDATE
After spending few more hours looking into the issue I found out that dynamic_cast
fails when it is used on the object created in another module.
From this
Using clang++, -fvisibility=hidden, and typeinfo, and type-erasure and this posts it looks like dynamic_cast
should work if the class is labeled with __attribute__((visibility("default")))
so that typeid
structures from two modules will be merged. This, however does not help either and I am still seeing the issue.