diff --git a/src/App.cpp b/src/App.cpp index fa8eeba..298388a 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -41,6 +41,7 @@ void App::run() { for(auto it = modules.begin(); it != modules.end(); it++) { (*it)->run(); } + closeModules(); } } @@ -76,19 +77,25 @@ void App::load(std::string lib) { } void App::unload(decltype(Module::self) it) { - void* handle = (*it)->getHandle(); - std::cout << "retrieved handle\nAttempting to delete module: " << (*it)->getName() << std::endl; + toClose.push_back((*it)->getHandle()); + std::cout << "marked handle for closure\n"; + std::cout << "Attempting to delete module: " << (*it)->getName() << std::endl; Module* m = *it; *it = nullptr; modules.erase(it); std::cout << "erased iterator\n"; delete m; std::cout << "deleted module\n"; - dlclose(handle); - std::cout << "closed handle\n"; } +void App::closeModules() { + for(auto it = toClose.begin(); it != toClose.end(); it++) { + dlclose(*it); + it = toClose.erase(it); + } +} + void App::handleArgs(const int& argc, char* argv[]) { int i = 1; diff --git a/src/App.h b/src/App.h index cd5d378..3d620b4 100644 --- a/src/App.h +++ b/src/App.h @@ -13,6 +13,7 @@ class App { bool done = false; std::list modules; + std::list toClose; public: App(const int&, char*[]); @@ -26,6 +27,8 @@ class App { void unload(decltype(Module::self)); + void closeModules(); + void handleArgs(const int&, char*[]); void printHelp();