Robotics - [Bugs - 2] SLAM Related Small Bugs

Passing Ptr By Value

Posted by Rico's Nerd Cluster on February 1, 2025

⚠️ Beware of Passing Smart Pointers by Value

Passing a smart pointer (e.g. std::shared_ptr or boost::shared_ptr) by value is fine when you’re only modifying the object it points to. But if you intend to reassign or reset the pointer itself (e.g. with .reset()), the changes won’t be visible to the caller — you’re just modifying a copy. Example:

1
2
3
4
5
6
7
8
9
10
11
12
void extract(const PCLFullCloudPtr full_cloud, PCLCloudXYZIPtr edge_points, PCLCloudXYZIPtr planar_points) const{
    if (edge_points == nullptr) {
        edge_points.reset(new PCLCloudXYZI);
    }
    if (planar_points == nullptr) {
        planar_points.reset(new PCLCloudXYZI);
    }
    ...
}
PCLCloudXYZIPtr edge_points = nullptr;
PCLCloudXYZIPtr planar_points = nullptr;
extractor.extract(scan_ptr, edge_points, planar_points);`