Index: lib/Support/Unix/DynamicLibrary.inc =================================================================== --- lib/Support/Unix/DynamicLibrary.inc +++ lib/Support/Unix/DynamicLibrary.inc @@ -15,7 +15,8 @@ #include DynamicLibrary::HandleSet::~HandleSet() { - for (void *Handle : Handles) + // Close the libraries in reverse order. + for (void *Handle : llvm::reverse(Handles)) ::dlclose(Handle); if (Process) ::dlclose(Process); Index: lib/Support/Windows/DynamicLibrary.inc =================================================================== --- lib/Support/Windows/DynamicLibrary.inc +++ lib/Support/Windows/DynamicLibrary.inc @@ -23,7 +23,7 @@ DynamicLibrary::HandleSet::~HandleSet() { - for (void *Handle : Handles) + for (void *Handle : llvm::reverse(Handles)) FreeLibrary(HMODULE(Handle)); // 'Process' should not be released on Windows.