Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/class_loader_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +53 to +61

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than forward-declaring it like this, can we just move the getLoaderMutex block down below up here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


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()
{
Expand Down
9 changes: 9 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
49 changes: 49 additions & 0 deletions test/multi_library_exit_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <string>

#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));
}
Loading