Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 13b8690

Browse files
authored
Avoid modifying loaded library map while iterating in lib_close() (#20941)
* Update close libs to not modify map while iterating over opened libraries, rename loaded_libs to loaded_libs_ to signify it is a private member. * Clean up and simplify library close code. * Fix clang-format.
1 parent 7a49008 commit 13b8690

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

src/initialize.cc

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ LibraryInitializer::LibraryInitializer()
103103
LibraryInitializer::~LibraryInitializer() = default;
104104

105105
bool LibraryInitializer::lib_is_loaded(const std::string& path) const {
106-
return loaded_libs.count(path) > 0;
106+
return loaded_libs_.count(path) > 0;
107107
}
108108

109109
/*!
@@ -139,9 +139,9 @@ void* LibraryInitializer::lib_load(const char* path) {
139139
}
140140
#endif // _WIN32 or _WIN64 or __WINDOWS__
141141
// then store the pointer to the library
142-
loaded_libs[path] = handle;
142+
loaded_libs_[path] = handle;
143143
} else {
144-
handle = loaded_libs.at(path);
144+
handle = loaded_libs_.at(path);
145145
}
146146
return handle;
147147
}
@@ -150,15 +150,7 @@ void* LibraryInitializer::lib_load(const char* path) {
150150
* \brief Closes the loaded dynamic shared library file
151151
* \param handle library file handle
152152
*/
153-
void LibraryInitializer::lib_close(void* handle) {
154-
std::string libpath;
155-
for (const auto& l : loaded_libs) {
156-
if (l.second == handle) {
157-
libpath = l.first;
158-
break;
159-
}
160-
}
161-
CHECK(!libpath.empty());
153+
void LibraryInitializer::lib_close(void* handle, const std::string& libpath) {
162154
#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
163155
FreeLibrary((HMODULE)handle);
164156
#else
@@ -167,7 +159,6 @@ void LibraryInitializer::lib_close(void* handle) {
167159
<< " loaded from: '" << libpath << "': " << dlerror();
168160
}
169161
#endif // _WIN32 or _WIN64 or __WINDOWS__
170-
loaded_libs.erase(libpath);
171162
}
172163

173164
/*!
@@ -393,9 +384,10 @@ SIGNAL_HANDLER(SIGBUS, SIGBUSHandler, false);
393384
#endif
394385

395386
void LibraryInitializer::close_open_libs() {
396-
for (const auto& l : loaded_libs) {
397-
lib_close(l.second);
387+
for (const auto& l : loaded_libs_) {
388+
lib_close(l.second, l.first);
398389
}
390+
loaded_libs_.clear();
399391
}
400392

401393
/**

src/initialize.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class LibraryInitializer {
6464
// Library loading
6565
bool lib_is_loaded(const std::string& path) const;
6666
void* lib_load(const char* path);
67-
void lib_close(void* handle);
67+
void lib_close(void* handle, const std::string& libpath);
6868
static void get_sym(void* handle, void** func, const char* name);
6969

7070
/**
@@ -104,7 +104,7 @@ class LibraryInitializer {
104104

105105
void close_open_libs();
106106

107-
loaded_libs_t loaded_libs;
107+
loaded_libs_t loaded_libs_;
108108
};
109109

110110
/*!

0 commit comments

Comments
 (0)