Skip to content

Commit 998df17

Browse files
committed
Link lifetime of SensorParser to lifetime of created sensor instances
This is required for parsers loaded via pluginlib to avoid premature unloading of the lib.
1 parent 939ec53 commit 998df17

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

urdf_parser/include/urdf_parser/sensor_parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace urdf {
5050
class URDFDOM_DLLAPI SensorParser {
5151
public:
5252
virtual ~SensorParser() = default;
53-
virtual SensorBaseSharedPtr parse(TiXmlElement &sensor_element) = 0;
53+
virtual SensorBase* parse(TiXmlElement &sensor_element) = 0;
5454
};
5555
URDF_TYPEDEF_CLASS_POINTER(SensorParser);
5656

urdf_parser/include/urdf_parser/visual_sensor_parsers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ namespace urdf {
4343

4444
class URDFDOM_DLLAPI CameraParser : public SensorParser {
4545
public:
46-
SensorBaseSharedPtr parse(TiXmlElement &sensor_element);
46+
SensorBase* parse(TiXmlElement &sensor_element);
4747
};
4848

4949
class URDFDOM_DLLAPI RayParser : public SensorParser {
5050
public:
51-
SensorBaseSharedPtr parse(TiXmlElement &sensor_element);
51+
SensorBase* parse(TiXmlElement &sensor_element);
5252
};
5353

5454
}

urdf_parser/src/sensor_parser.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ SensorBaseSharedPtr parseSensorBase(TiXmlElement *sensor_xml, const SensorParser
6161
SensorParserMap::const_iterator parser = parsers.find(sensor_type);
6262
if (parser != parsers.end() && parser->second)
6363
{
64-
return parser->second->parse(*sensor_base_xml);
64+
// Link sensor's deleter to the parser.
65+
// If the parser was loaded via a pluginlib, this is required to link
66+
// the lifetime of the parser/library to the created sensor instances.
67+
return SensorBaseSharedPtr(parser->second->parse(*sensor_base_xml),
68+
[linked_parser = parser->second](auto *p)
69+
{ delete p; });
6570
}
6671
else
6772
{

urdf_parser/src/visual_sensor_parsers.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,73 +44,73 @@
4444

4545
namespace urdf {
4646

47-
SensorBaseSharedPtr CameraParser::parse(TiXmlElement &config)
47+
SensorBase* CameraParser::parse(TiXmlElement &config)
4848
{
4949
TiXmlElement *image = config.FirstChildElement("image");
5050
if (image)
5151
{
5252
try {
53-
CameraSharedPtr camera(new Camera());
53+
std::unique_ptr<Camera> camera(new Camera());
5454
camera->width = parseAttribute<unsigned int>(*image, "width");
5555
camera->height = parseAttribute<unsigned int>(*image, "height");
5656
camera->format = parseAttribute<std::string>(*image, "format");
5757
camera->hfov = parseAttribute<double>(*image, "hfov");
5858
camera->near = parseAttribute<double>(*image, "near");
5959
camera->far = parseAttribute<double>(*image, "far");
60-
return camera;
60+
return camera.release();
6161
}
6262
catch (const std::exception &e)
6363
{
6464
CONSOLE_BRIDGE_logError("Camera sensor %s", e.what());
65-
return CameraSharedPtr();
65+
return nullptr;
6666
}
6767
}
6868
else
6969
{
7070
CONSOLE_BRIDGE_logError("Camera sensor has no <image> element");
71-
return CameraSharedPtr();
71+
return nullptr;
7272
}
7373
}
7474

7575

76-
SensorBaseSharedPtr RayParser::parse(TiXmlElement &config)
76+
SensorBase* RayParser::parse(TiXmlElement &config)
7777
{
7878
TiXmlElement *horizontal = config.FirstChildElement("horizontal");
7979
if (horizontal)
8080
{
8181
try {
82-
RaySharedPtr ray (new Ray());
82+
std::unique_ptr<Ray> ray(new Ray());
8383
ray->horizontal_samples = parseAttribute<unsigned int>(*horizontal, "samples");
8484
ray->horizontal_resolution = parseAttribute<double>(*horizontal, "resolution");
8585
ray->horizontal_min_angle = parseAttribute<double>(*horizontal, "min_angle");
8686
ray->horizontal_max_angle = parseAttribute<double>(*horizontal, "max_angle");
87-
return ray;
87+
return ray.release();
8888
}
8989
catch (const std::exception &e)
9090
{
9191
CONSOLE_BRIDGE_logError("Ray horizontal: %s", e.what());
92-
return RaySharedPtr();
92+
return nullptr;
9393
}
9494
}
9595

9696
TiXmlElement *vertical = config.FirstChildElement("vertical");
9797
if (vertical)
9898
{
9999
try {
100-
RaySharedPtr ray (new Ray());
100+
std::unique_ptr<Ray> ray(new Ray());
101101
ray->vertical_samples = parseAttribute<unsigned int>(*vertical, "samples");
102102
ray->vertical_resolution = parseAttribute<double>(*vertical, "resolution");
103103
ray->vertical_min_angle = parseAttribute<double>(*vertical, "min_angle");
104104
ray->vertical_max_angle = parseAttribute<double>(*vertical, "max_angle");
105-
return ray;
105+
return ray.release();
106106
}
107107
catch (const std::exception &e)
108108
{
109109
CONSOLE_BRIDGE_logError("Ray horizontal: %s", e.what());
110-
return RaySharedPtr();
110+
return nullptr;
111111
}
112112
}
113-
return RaySharedPtr();
113+
return nullptr;
114114
}
115115

116116
}

0 commit comments

Comments
 (0)