Skip to content

Commit

Permalink
don't use a static ClassLoader instance
Browse files Browse the repository at this point in the history
- crashes on exit: ros/class_loader#33
- on-demand-unloading works with ros/class_loader#34
  • Loading branch information
rhaschke committed Sep 21, 2022
1 parent 42af95b commit 4aa551e
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions urdf/src/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,28 @@ SensorMap parseSensors(const std::string &xml_string, const SensorParserMap &par

SensorParserMap getSensorParsers(const std::vector<std::string> &allowed)
{
static boost::mutex PARSER_PLUGIN_LOCK;
static boost::shared_ptr<pluginlib::ClassLoader<urdf::SensorParser> > PARSER_PLUGIN_LOADER;
boost::mutex::scoped_lock _(PARSER_PLUGIN_LOCK);

pluginlib::ClassLoader<urdf::SensorParser> loader("urdf", "urdf::SensorParser");
SensorParserMap parserMap;
try
{
if (!PARSER_PLUGIN_LOADER)
PARSER_PLUGIN_LOADER.reset(new pluginlib::ClassLoader<urdf::SensorParser>("urdf", "urdf::SensorParser"));

const std::vector<std::string> &classes = PARSER_PLUGIN_LOADER->getDeclaredClasses();
for (std::size_t i = 0 ; i < classes.size() ; ++i)
{
// skip this class if not listed in allowed
if (!allowed.empty() && std::find(allowed.begin(), allowed.end(), classes[i]) == allowed.end())
continue;

urdf::SensorParserSharedPtr parser;
try {
parser = PARSER_PLUGIN_LOADER->createInstance(classes[i]);
} catch(const pluginlib::PluginlibException& ex) {
ROS_ERROR_STREAM("Failed to create sensor parser: " << classes[i] << "\n" << ex.what());
}
parserMap.insert(std::make_pair(classes[i], parser));
ROS_DEBUG_STREAM("added sensor parser: " << classes[i]);
const std::vector<std::string> &classes = loader.getDeclaredClasses();
for (std::size_t i = 0 ; i < classes.size() ; ++i)
{
// skip this class if not listed in allowed
if (!allowed.empty() && std::find(allowed.begin(), allowed.end(), classes[i]) == allowed.end())
continue;

urdf::SensorParserSharedPtr parser;
try {
parser = loader.createInstance(classes[i]);
} catch(const pluginlib::PluginlibException& ex) {
ROS_ERROR_STREAM("Failed to create sensor parser: " << classes[i] << "\n" << ex.what());
}
if (parserMap.empty())
ROS_WARN_STREAM("No sensor parsers found");
parserMap.insert(std::make_pair(classes[i], parser));
ROS_DEBUG_STREAM("added sensor parser: " << classes[i]);
}
if (parserMap.empty())
ROS_WARN_STREAM("No sensor parsers found");
}
catch(const pluginlib::PluginlibException& ex)
{
Expand Down

0 comments on commit 4aa551e

Please sign in to comment.