Dlopen und vector (Gelöst)
-
Moin,
versuche gerade ein Object einer klasse in einen Vector zu laden, leider bekomme ich immer wieder einen Zugriffsfehler.
Update:
Habe auf shared_ptr umgestellt leider mit dem gleichen Ergebnis.Update:
Bin bißchen doof habe in ApiExt ein Module=Modules.begin(); vergessen.Valgrind:
==22198== Process terminating with default action of signal 11 (SIGSEGV) ==22198== Access not within mapped region at address 0x0 ==22198== at 0x406BCE: ApiExt::setRequest(char const*, unsigned long) (api.cpp:188) ==22198== by 0x405FCD: Api::Daemon() (api.cpp:66) ==22198== by 0x405AE8: main (main.cpp:43) ==22198== If you believe this happened as a result of a stack ==22198== overflow in your program's main thread (unlikely but ==22198== possible), you can try to increase the size of the ==22198== main thread stack using the --main-stacksize= flag. ==22198== The main thread stack size used in this run was 8388608.
modul.h
class ApiModul { public: virtual void setRequest(const char *req,size_t dsize=0)=0; virtual const char *getResponse()=0; virtual size_t getResponseSize()=0; }; struct ModulPtr { std::shared_ptr<ApiModul> Modul; std::shared_ptr<void> Sym; }; class ApiExt { public: ApiExt(const char *path); ~ApiExt(); bool nextModul(); void setRequest(const char *req,size_t dsize=0); const char *getResponse(); size_t getResponseSize(); private: std::vector<ModulPtr> Modules; std::vector<ModulPtr>::iterator Module; typedef void destroy_t(ApiModul*); };
modul.cpp
ApiExt::ApiExt(const char *path){ if(path==0) return; DIR *dirptr; struct dirent *dir; if ((dirptr=opendir(path)) == 0) return; while((dir=readdir(dirptr)) != 0){ if(dir->d_type==DT_REG && dir->d_name!=0){ ModulPtr modulptr; modulptr.Sym=(std::shared_ptr<void>) dlopen(std::string(MODULPATH+std::string(dir->d_name)).c_str(), RTLD_LAZY); if(!modulptr.Sym) { std::cerr << "Cannot load Modul: " << dlerror() << '\n'; }else{ dlerror(); typedef ApiModul* create_t(); create_t* create_module = (create_t*) dlsym(modulptr.Sym.get(), "create"); const char* dlsym_error = dlerror(); if (dlsym_error) { std::cerr << "Cannot load symbol create: " << dlsym_error << '\n'; }else{ modulptr.Modul=(std::shared_ptr<ApiModul>)create_module(); Modules.push_back(modulptr); } } } } closedir(dirptr); return; } ApiExt::~ApiExt(){ Module=Modules.begin(); do{ destroy_t* destroy_module = (destroy_t*) dlsym(Module->Sym.get(), "destroy"); destroy_module(Module->Modul.get()); dlclose(Module->Sym.get()); }while(nextModul()==false); } bool ApiExt::nextModul(){ if(Modules.empty()) return false; if(Module!=Modules.end()){ Module++; return true; } Module=Modules.begin(); return false; } void ApiExt::setRequest(const char* req, size_t dsize){ Module->Modul->setRequest(req,dsize); } const char* ApiExt::getResponse(){ // return Module->Modul->getResponse(); return 0; } size_t ApiExt::getResponseSize(){ // return Module->Modul->getResponseSize(); }
-
Es muss anscheinend am Iterator liegen denn wenn ich das Modul so aufrufe gehts:
Modules.begin()->Modul->setRequest(req,dsize);