diff --git a/src/class_loader_core.cpp b/src/class_loader_core.cpp index d59afd2..6f0f71e 100644 --- a/src/class_loader_core.cpp +++ b/src/class_loader_core.cpp @@ -50,27 +50,29 @@ namespace impl // Global data -std::recursive_mutex & getLoadedLibraryVectorMutex() +namespace +{ +// Serializes loadLibrary()/unloadLibrary() so a dlopen() never overlaps a dlclose(). +std::recursive_mutex & getLoaderMutex() { static std::recursive_mutex m; return m; } +} // namespace -std::recursive_mutex & getPluginBaseToFactoryMapMapMutex() +std::recursive_mutex & getLoadedLibraryVectorMutex() { + // Construct the loader mutex first so it is destroyed last: unloadLibrary() locks it. + (void)getLoaderMutex(); static std::recursive_mutex m; return m; } -namespace -{ -// Serializes loadLibrary()/unloadLibrary() so a dlopen() never overlaps a dlclose(). -std::recursive_mutex & getLoaderMutex() +std::recursive_mutex & getPluginBaseToFactoryMapMapMutex() { static std::recursive_mutex m; return m; } -} // namespace BaseToFactoryMapMap & getGlobalPluginBaseToFactoryMapMap() { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7eee7fb..dea7701 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -76,4 +76,13 @@ if(TARGET ${PROJECT_NAME}_external_load_test) ${PROJECT_NAME}_TestPlugins2) endif() +ament_add_gtest(${PROJECT_NAME}_multi_library_exit_test multi_library_exit_test.cpp + APPEND_LIBRARY_DIRS "${append_library_dirs}" +) +if(TARGET ${PROJECT_NAME}_multi_library_exit_test) + target_include_directories(${PROJECT_NAME}_multi_library_exit_test PUBLIC "../include") + target_link_libraries(${PROJECT_NAME}_multi_library_exit_test ${PROJECT_NAME}) + add_dependencies(${PROJECT_NAME}_multi_library_exit_test ${PROJECT_NAME}_TestPlugins1) +endif() + add_subdirectory(fviz_case_study) diff --git a/test/multi_library_exit_test.cpp b/test/multi_library_exit_test.cpp new file mode 100644 index 0000000..42d63bb --- /dev/null +++ b/test/multi_library_exit_test.cpp @@ -0,0 +1,49 @@ +/* + * Software License Agreement (BSD License) + * + * Copyright (c) 2026, Open Source Robotics Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include "class_loader/class_loader.hpp" +#include "class_loader/multi_library_class_loader.hpp" + +#include "gtest/gtest.h" + +// Regression test: unloadLibrary()'s static mutexes must outlive the static loader vector. +// Keep this a separate executable: the MultiLibraryClassLoader has to be the first user of +// class_loader, and the loader it creates is destroyed during static destruction. + +TEST(ClassLoaderExitTest, loaderKeptAliveUntilStaticDestruction) { + // Never destroyed on purpose, so the loader outlives main(). + static auto * loader = new class_loader::MultiLibraryClassLoader(false); + const std::string library = class_loader::systemLibraryFormat("class_loader_TestPlugins1"); + loader->loadLibrary(library); + EXPECT_TRUE(loader->isLibraryAvailable(library)); +}