Dynamic Cast C++ Fail
Dynamic Cast C++ Fail
我在一个g++编译器(Redhat 5.5 gcc 3.4.6版)上遇到了动态强制转换失败,该编译器在Windows Visual studio 2003、2005和2010编译器上运行良好。为了理解我所看到的情况,我会尽快把问题分解。我们有一个从目录加载大量"插件"并动态加载这些插件(它们是动态链接的库)的过程。该过程应该比较不同的规则和数据类型以返回答案。为了使流程模块化,我们让流程了解"BaseDataType",但不了解实际的特定类型(因此我们可以保持流程的通用性)。程序的流程是这样的:
我们所有的"SpecifcObject"类型都继承自类似的"BaseDataType"
class SpecificObject : public virtual BaseDataType {
... Class Items ...
}
这是来自过程的代码看起来像:
// Receive raw data
void receive_data(void *buff, int size,DataTypeEnum type)
{
// Get the plugin associated with this data
ProcessPlugin *plugin = m_plugins[type];
// Since we need to cast the data properly into its actual type and not its
// base data type we need the plugin to cast it for us (so we can keep the
// process generic)
BaseDataType *data = plugin->getDataObject(buff);
if(data)
{
// Cast worked just fine
.... Other things happen (but object isn't modified) ....
// Now compare our rule
RuleObject obj = getRule();
ResultObject *result = plugin->CompareData(obj,data);
if(result)
... Success Case ...
else
... Error Case ...
}
}
现在,这就是(一般)插件的样子
BaseDataType* ProcessPluginOne::getDataObject(unsigned char *buff)
{
// SpecificObject inherits from BaseDataType using a "virtual" inheritance
SpecificObject *obj = reinterpret_cast<SpecificObject*>(buff);
if(obj)
return (BaseDataType*)obj;
else
return NULL;
}
ResultObject* ProcessPluginOne::CompareData(RuleObject rule, BaseDataType *data)
{
ResultObject *obj = NULL;
// This method checks out fine
if(data->GetSomeBaseMethod())
{
// This cast below FAILS every time in gcc but passes in Visual Studio
SpecificObject *obj = dynamic_cast<SpecificObject*>(data);
if(obj)
{
... Do Something ...
}
}
return result;
}
同样,所有这些都可以在VisualStudio下工作,但不能在GCC下工作。为了调试程序,我开始在不同的部分添加一些代码。当我在主流程中完成以下操作时,我终于让它工作了(见下面添加的代码):
// In process with Modification
void receive_data(void *buff, int size,DataTypeEnum type)
{
// Get the plugin associated with this data
ProcessPlugin *plugin = m_plugins[type];
// Since we need to cast the data properly into its actual type and not its
// base data type we need the plugin to cast it for us (so we can keep the
// process generic)
BaseDataType *data = plugin->getDataObject(buff);
if(data)
{
// Cast worked just fine
.... Other things happen (but object isn't modified) ....
// Now compare our rule
RuleObject obj = getRule();
/** I included the specific data types in as headers for debugging and linked in
* all the specific classes and added the following code
*/
SpecificObject *test_obj = dynamic_cast<SpecificObject*>(data);
if(test_obj)
cout << "Our was Data was casted correctly!" << endl;
/// THE CODE ABOVE FIXES THE BAD CAST IN MY PLUGIN EVEN THOUGH
/// THE CODE ABOVE IS ALL I DO
ResultObject *result = plugin->CompareData(obj,data);
if(result)
... Success Case ...
else
... Error Case ...
}
}
重要过程编译选项:
Compile: -m64 -fPIC -wno-non-template-friend -DNDEGBUG -I <Includes>
Link: -Wl -z muldefs -m64
重要插件编译选项
Compile: -Wall -wno-non-template-friend -O -O2
Link: -Wl -Bstatic -Bdynamic -z muldefs -shared -m64
由于我没有修改对象"数据",我不知道为什么程序的其余部分会突然开始工作。我唯一能想到的是,虚拟表在这个过程中的某个地方被剥离了,"额外"的动态演员阵容迫使主进程保留表(这仍然没有多大意义)。
我试着去掉了gcc中的所有优化设置,它仍然是一样的。有什么想法吗?
谢谢。
两种最可能的情况是CCD_ 1不是多态类型,或者编译器在代码中的某个时刻看不到BaseDataType
和SpecificObject
之间的关系(例如,在getDataObject
中,编译器可能会根据父子关系的知识生成不同的代码,因为从子级到父级使用C-cast
。这非常容易检查:将C转换更改为static_cast
。如果编译失败,则缺少一个关键的include。
- Problems with std::cin.fail()
- 了解 cin.fail() 和 cin.clear语言 - Vector 追加程序
- cin.fail() not returning true
- cin.fail 在输入数字和字母时不起作用
- 我如何检查CIN.FAIL(),但仍使用Ctrl D到达文档的结尾
- OpenCV UIImageToMat linker fail in xcode
- CIN.FAIL无法按预期工作
- Qt WebEngine on fail
- 在C++流中,eof(),fail(),bad()和good()有什么区别
- ICLRRuntimeHost::UnloadAppDomain fail
- 如何使用CIN.FAIL()杀死程序
- What value should `std::stringstream::fail()` return after r
- 检测在ofstream写入过程中的空间不足,stream.fail()无法正常工作
- sgx_ecc256_create_key_pair fail
- 有没有办法将cin.fail和cin.clear翻译成C编程语言
- 如何在cin.fail()中求解循环
- 我是否以不良方式使用ifstream :: fail()方法
- C++ cin.fail() while loop
- 函数中的ifstream var.fail()
- 为什么 cin.fail() 在输入数字后跟字符时不返回 false?