gcc - Dynamic Cast C++ Fail -
i having dynamic cast fail on g++ compiler (redhat 5.5 gcc version 3.4.6) works fine on windows visual studio 2003, 2005, , 2010 compiler. understand seeing try break problem down. have process loads in numerous "plugins" directory , dynamically loads these plugins (which dynamically linked libraries). process supposed compare different rules , data types return answer. make process modulure made process understand "basedatatype" not actual specific types (so keep process generic). flow of program goes this:
all our "specifcobject" types inherit "basedatatype" this
class specificobject : public virtual basedatatype { ... class items ... }
this code process looks like:
// receive raw data void receive_data(void *buff, int size,datatypeenum type) { // plugin associated data processplugin *plugin = m_plugins[type]; // since need cast data actual type , not // base data type need plugin cast (so can keep // process generic) basedatatype *data = plugin->getdataobject(buff); if(data) { // cast worked fine .... other things happen (but object isn't modified) .... // compare our rule ruleobject obj = getrule(); resultobject *result = plugin->comparedata(obj,data); if(result) ... success case ... else ... error case ... } }
now (generically) plugin like
basedatatype* processpluginone::getdataobject(unsigned char *buff) { // specificobject inherits basedatatype using "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; // method checks out fine if(data->getsomebasemethod()) { // cast below fails every time in gcc passes in visual studio specificobject *obj = dynamic_cast<specificobject*>(data); if(obj) { ... ... } } return result; }
again of works under visual studio not under gcc. debug program started adding code different sections. got work once did following in main process (see added code below):
// in process modification void receive_data(void *buff, int size,datatypeenum type) { // plugin associated data processplugin *plugin = m_plugins[type]; // since need cast data actual type , not // base data type need plugin cast (so can keep // process generic) basedatatype *data = plugin->getdataobject(buff); if(data) { // cast worked fine .... other things happen (but object isn't modified) .... // compare our rule ruleobject obj = getrule(); /** included specific data types in headers debugging , linked in * specific classes , added following code */ specificobject *test_obj = dynamic_cast<specificobject*>(data); if(test_obj) cout << "our data casted correctly!" << endl; /// code above fixes bad cast in plugin though /// code above resultobject *result = plugin->comparedata(obj,data); if(result) ... success case ... else ... error case ... } }
significant process compile options:
compile: -m64 -fpic -wno-non-template-friend -dndegbug -i <includes> link: -wl -z muldefs -m64
significant plugin compile options
compile: -wall -wno-non-template-friend -o -o2 link: -wl -bstatic -bdynamic -z muldefs -shared -m64
since not modifying object "data" have no idea why rest of program start working. thing can think of virtual table getting stripped off somewhere in process , "extra" dynamic cast forces main process keep table (which still doesn't make lot of sense).
i have tried taking out optimization settings in gcc , still same. thoughts going on here?
thanks.
the 2 scenarios basedatatype
not polymorphic type, or compiler doesn't see relationship between basedatatype
, specificobject
@ point in code (for example, in getdataobject
compiler might generate different code depending on knowledge of parent-child relationship, since use c-cast
child parent. super easy check: change c-cast static_cast
. if fails compile you're missing critical include.
Comments
Post a Comment