Skip to content

Commit

Permalink
Link lifetime of SensorParser to lifetime of created sensor instances
Browse files Browse the repository at this point in the history
This is required for parsers loaded via pluginlib to avoid premature unloading of the lib.
  • Loading branch information
rhaschke committed Oct 18, 2022
1 parent 939ec53 commit 998df17
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion urdf_parser/include/urdf_parser/sensor_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace urdf {
class URDFDOM_DLLAPI SensorParser {
public:
virtual ~SensorParser() = default;
virtual SensorBaseSharedPtr parse(TiXmlElement &sensor_element) = 0;
virtual SensorBase* parse(TiXmlElement &sensor_element) = 0;
};
URDF_TYPEDEF_CLASS_POINTER(SensorParser);

Expand Down
4 changes: 2 additions & 2 deletions urdf_parser/include/urdf_parser/visual_sensor_parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ namespace urdf {

class URDFDOM_DLLAPI CameraParser : public SensorParser {
public:
SensorBaseSharedPtr parse(TiXmlElement &sensor_element);
SensorBase* parse(TiXmlElement &sensor_element);
};

class URDFDOM_DLLAPI RayParser : public SensorParser {
public:
SensorBaseSharedPtr parse(TiXmlElement &sensor_element);
SensorBase* parse(TiXmlElement &sensor_element);
};

}
Expand Down
7 changes: 6 additions & 1 deletion urdf_parser/src/sensor_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ SensorBaseSharedPtr parseSensorBase(TiXmlElement *sensor_xml, const SensorParser
SensorParserMap::const_iterator parser = parsers.find(sensor_type);
if (parser != parsers.end() && parser->second)
{
return parser->second->parse(*sensor_base_xml);
// Link sensor's deleter to the parser.
// If the parser was loaded via a pluginlib, this is required to link
// the lifetime of the parser/library to the created sensor instances.
return SensorBaseSharedPtr(parser->second->parse(*sensor_base_xml),
[linked_parser = parser->second](auto *p)
{ delete p; });
}
else
{
Expand Down
26 changes: 13 additions & 13 deletions urdf_parser/src/visual_sensor_parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,73 +44,73 @@

namespace urdf {

SensorBaseSharedPtr CameraParser::parse(TiXmlElement &config)
SensorBase* CameraParser::parse(TiXmlElement &config)
{
TiXmlElement *image = config.FirstChildElement("image");
if (image)
{
try {
CameraSharedPtr camera(new Camera());
std::unique_ptr<Camera> camera(new Camera());
camera->width = parseAttribute<unsigned int>(*image, "width");
camera->height = parseAttribute<unsigned int>(*image, "height");
camera->format = parseAttribute<std::string>(*image, "format");
camera->hfov = parseAttribute<double>(*image, "hfov");
camera->near = parseAttribute<double>(*image, "near");
camera->far = parseAttribute<double>(*image, "far");
return camera;
return camera.release();
}
catch (const std::exception &e)
{
CONSOLE_BRIDGE_logError("Camera sensor %s", e.what());
return CameraSharedPtr();
return nullptr;
}
}
else
{
CONSOLE_BRIDGE_logError("Camera sensor has no <image> element");
return CameraSharedPtr();
return nullptr;
}
}


SensorBaseSharedPtr RayParser::parse(TiXmlElement &config)
SensorBase* RayParser::parse(TiXmlElement &config)
{
TiXmlElement *horizontal = config.FirstChildElement("horizontal");
if (horizontal)
{
try {
RaySharedPtr ray (new Ray());
std::unique_ptr<Ray> ray(new Ray());
ray->horizontal_samples = parseAttribute<unsigned int>(*horizontal, "samples");
ray->horizontal_resolution = parseAttribute<double>(*horizontal, "resolution");
ray->horizontal_min_angle = parseAttribute<double>(*horizontal, "min_angle");
ray->horizontal_max_angle = parseAttribute<double>(*horizontal, "max_angle");
return ray;
return ray.release();
}
catch (const std::exception &e)
{
CONSOLE_BRIDGE_logError("Ray horizontal: %s", e.what());
return RaySharedPtr();
return nullptr;
}
}

TiXmlElement *vertical = config.FirstChildElement("vertical");
if (vertical)
{
try {
RaySharedPtr ray (new Ray());
std::unique_ptr<Ray> ray(new Ray());
ray->vertical_samples = parseAttribute<unsigned int>(*vertical, "samples");
ray->vertical_resolution = parseAttribute<double>(*vertical, "resolution");
ray->vertical_min_angle = parseAttribute<double>(*vertical, "min_angle");
ray->vertical_max_angle = parseAttribute<double>(*vertical, "max_angle");
return ray;
return ray.release();
}
catch (const std::exception &e)
{
CONSOLE_BRIDGE_logError("Ray horizontal: %s", e.what());
return RaySharedPtr();
return nullptr;
}
}
return RaySharedPtr();
return nullptr;
}

}

0 comments on commit 998df17

Please sign in to comment.