Skip to content

Commit

Permalink
fixes static initialization and destruction issue
Browse files Browse the repository at this point in the history
  • Loading branch information
eyck committed May 11, 2023
1 parent fa6166a commit 658fd7b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/iss/plugin/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@
#include "loader.h"

using namespace iss::plugin;
std::unordered_map<std::string, std::shared_ptr<loader::plugin_data>> loader::_cache;

std::shared_ptr<loader::plugin_data> loader::get_data(const std::string& filepath) {
if (filepath.empty())
throw std::invalid_argument("failed to bind to loader: filepath was empty");
auto iter = _cache.find(filepath);
if (iter != _cache.end())
auto iter = get_cache().find(filepath);
if (iter != get_cache().end())
return iter->second;
#if OS_IS_WINDOWS
auto handle = (void*)LoadLibrary(filepath.c_str());
Expand All @@ -80,7 +79,7 @@ std::shared_ptr<loader::plugin_data> loader::get_data(const std::string& filepat
+ std::string(dlerror()));
#endif
auto data = std::make_shared<plugin_data>(handle, filepath);
_cache[filepath] = data;
get_cache()[filepath] = data;
return data;
}

Expand Down
11 changes: 8 additions & 3 deletions src/iss/plugin/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class loader {
: handle(handle), filepath(filepath) { }

~plugin_data() {
symbols.clear();
if (handle) {
#if OS_IS_WINDOWS
FreeLibrary((HINSTANCE)handle);
Expand All @@ -122,7 +123,11 @@ class loader {
};

private: // members
static std::unordered_map<std::string, std::shared_ptr<plugin_data>> _cache;
using cache_t = std::unordered_map<std::string, std::shared_ptr<plugin_data>>;
static cache_t& get_cache() {
static cache_t cache;
return cache;
}
std::shared_ptr<plugin_data> _data;
std::shared_ptr<plugin_data> get_data(const std::string &filepath);

Expand All @@ -141,7 +146,7 @@ class loader {
}

loader(const loader &other)
: _data(_cache[other.filepath()]) {
: _data(get_cache()[other.filepath()]) {
}

void bind_function(const std::string &label);
Expand Down Expand Up @@ -176,7 +181,7 @@ class loader {
}

inline loader& operator=(const loader &other) {
_data = _cache[other.filepath()];
_data = get_cache()[other.filepath()];
return *this;
}

Expand Down

0 comments on commit 658fd7b

Please sign in to comment.