bipham has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am trying to use Boost::variant in XS. However I was not able to pass variant variable back and forth between Perl and C++. Would you please help? Any suggestion is greatly appreciated. My code is pasted below
typedef boost::variant<macro,module> ref_var; macro* nDB::create_macro(char* myName) { macro* tmp_macro = new macro; tmp_macro->set_name(myName); if (_BREF->insert(std::make_pair(myName,*tmp_macro)).second) { printf("Insert macro %s OK!\n",myName); } else { printf("Could not insert macro %s into database!\n",myName); } return (tmp_macro); } ref_var* nDB::lookup_ref(char* myName) { boost::unordered_map<char*,ref_var,myhash,cmp_str>::iterator _ITER; if ((_ITER = _BREF->find(myName)) == _BREF->end()) { printf("ERROR:: Could not find macro %s in database \n",myName) +; exit(0); } else { _ITER = _BREF->find(myName); printf("Ref %s is found\n",myName); } ref_var* ref = new ref_var; *ref = (_ITER->second); macro *mac = boost::get<macro>(ref); printf("Macro class is %s\n",mac->get_class()); return(ref); }
Below is my typemap
TYPEMAP ref_var* REF_VARIANT OUTPUT REF_VARIANT char* type = get_ref_type(*$var); if (std::strcmp(type,"macro")) { printf ("HEY"); macro* mac = new macro; mac = boost::get<macro>(&$var); printf("Macro class is %s",mac->get_class()); sv_setref_pv($arg,"macro",mac); } else { module* mod = new module; mod = boost::get<module>(&$var); sv_setref_pv($arg,"module",mod); }
And here is what I got from Perl
TEST MACRO Insert macro macro1 OK! Insert macro macro2 OK! Insert macro macro3 OK! Insert port PA OK! Insert port P2 OK! Insert port P3 OK! Ref macro2 is found Macro class is COVER *** glibc detected *** free(): invalid pointer: 0x0000000000910f90 *** Abort
Would you please tell me what did I do wrong? I have spent the whole week on this trying many solutions I could think of but couldn't succeed. Thank you very much for your help!