|
33 | 33 | #include <log/log.h> |
34 | 34 |
|
35 | 35 | #include <filesystem> |
| 36 | +#include <map> |
36 | 37 | #include <set> |
37 | 38 | #include <string> |
38 | 39 | #include <vector> |
@@ -63,6 +64,8 @@ union loader_impl_function_cast |
63 | 64 | int (*fn)(void *, void *); |
64 | 65 | }; |
65 | 66 |
|
| 67 | +static std::map<std::string, loader_impl_ext_handle_lib_type> destroy_list; |
| 68 | + |
66 | 69 | dynlink ext_loader_impl_load_from_file_dynlink(loader_impl_ext ext_impl, const loader_path path); |
67 | 70 | int ext_loader_impl_load_from_file_handle(loader_impl_ext ext_impl, loader_impl_ext_handle ext_handle, const loader_path path); |
68 | 71 | static void ext_loader_impl_destroy_handle(loader_impl_ext_handle ext_handle); |
@@ -143,6 +146,14 @@ dynlink ext_loader_impl_load_from_file_dynlink(loader_impl_ext ext_impl, const l |
143 | 146 |
|
144 | 147 | int ext_loader_impl_load_from_file_handle(loader_impl_ext ext_impl, loader_impl_ext_handle ext_handle, const loader_path path) |
145 | 148 | { |
| 149 | + auto iterator = destroy_list.find(path); |
| 150 | + if (iterator != destroy_list.end()) |
| 151 | + { |
| 152 | + log_write("metacall", LOG_LEVEL_DEBUG, "Unloading handle: %s <%p>", iterator->second.name.c_str(), iterator->second.handle); |
| 153 | + dynlink_unload(iterator->second.handle); |
| 154 | + destroy_list.erase(path); |
| 155 | + } |
| 156 | + |
146 | 157 | dynlink lib = ext_loader_impl_load_from_file_dynlink(ext_impl, path); |
147 | 158 |
|
148 | 159 | if (lib == NULL) |
@@ -256,7 +267,14 @@ int ext_loader_impl_clear(loader_impl impl, loader_handle handle) |
256 | 267 |
|
257 | 268 | if (ext_handle != NULL) |
258 | 269 | { |
259 | | - ext_loader_impl_destroy_handle(ext_handle); |
| 270 | + for (size_t i = 0; i < ext_handle->extensions.size(); i++) |
| 271 | + { |
| 272 | + log_write("metacall", LOG_LEVEL_DEBUG, "Storing handle: %s <%p> in destroy list", ext_handle->extensions[i].name.c_str(), ext_handle->extensions[i].handle); |
| 273 | + destroy_list[ext_handle->extensions[i].name] = ext_handle->extensions[i]; |
| 274 | + ext_handle->extensions.erase(ext_handle->extensions.begin() + i); |
| 275 | + } |
| 276 | + |
| 277 | + delete ext_handle; |
260 | 278 |
|
261 | 279 | return 0; |
262 | 280 | } |
@@ -294,6 +312,15 @@ int ext_loader_impl_destroy(loader_impl impl) |
294 | 312 |
|
295 | 313 | delete ext_impl; |
296 | 314 |
|
| 315 | + /* Destroy all handles */ |
| 316 | + if (!destroy_list.empty()) |
| 317 | + { |
| 318 | + for (auto iterator : destroy_list) |
| 319 | + { |
| 320 | + log_write("metacall", LOG_LEVEL_DEBUG, "Unloading handle: %s <%p>", iterator.second.name.c_str(), iterator.second.handle); |
| 321 | + dynlink_unload(iterator.second.handle); |
| 322 | + } |
| 323 | + } |
297 | 324 | return 0; |
298 | 325 | } |
299 | 326 |
|
|
0 commit comments