Dynamic_cast C ++失败

时间:2013-07-17 17:41:08

标签: c++ gcc

我在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;
}

同样,所有这些都在Visual Studio下工作,但不在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中取出所有优化设置,但仍然相同。关于这里发生了什么的任何想法?

感谢。

1 个答案:

答案 0 :(得分:1)

两种最可能的情况是BaseDataType不是多态类型,或者编译器在代码中的某个点上没有看到BaseDataTypeSpecificObject之间的关系(例如,在getDataObject中,编译器可能会根据父子关系的知识生成不同的代码,因为您从子级到父级使用C-cast。这非常容易检查:更改C-cast到static_cast。如果编译失败,你就错过了一个关键的包含。