Skip to content

Commit d14cd25

Browse files
authored
Fixed double free error (#327)
Signed-off-by: Chuck Ketcham <[email protected]>
1 parent e02b569 commit d14cd25

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

libs/qec/lib/plugin_loader.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ namespace fs = std::filesystem;
1515
namespace cudaq::qec {
1616

1717
static std::map<std::string, PluginHandle> &get_plugin_handles() {
18-
static std::map<std::string, PluginHandle> plugin_handles;
19-
return plugin_handles;
18+
// Formerly this code was like this:
19+
// static std::map<std::string, PluginHandle> plugin_handles;
20+
// return plugin_handles;
21+
// But that created a double free error when the program exited.
22+
// This was because the static destructor for the map was called first,
23+
// and then the cleanup_plugins function was called which tried to access the
24+
// already destroyed map. This new approach does create a small memory leak,
25+
// but it prevents the double free error.
26+
static auto *plugin_handles = new std::map<std::string, PluginHandle>();
27+
return *plugin_handles; // Dereference pointer to return reference
2028
}
2129

2230
// Function to load plugins from a directory based on their type

0 commit comments

Comments
 (0)