Skip to content

Commit

Permalink
FIX: race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Jan 13, 2024
1 parent 624934a commit 74598e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/source/doxygen-docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Fix wrong filenames in `rawlog-edit --externalize` when sensor labels contain the `/` character (e.g. mimicking ROS topic names).
- Fix crash in mrpt::ros2bridge::toROS() for XYZIRT point clouds.
- Fix exception while rendering paths in the `ptg-configurator` application.
- Fix potential race condition in mrpt::obs::CObservation3DRangeScan.

# Version 2.11.5: Released Dec 21st, 2023
- Changes in libraries:
Expand Down
31 changes: 23 additions & 8 deletions libs/obs/src/CObservation3DRangeScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,44 @@ struct hash<LUT_info>
};
} // namespace std

static std::unordered_map<LUT_info, CObservation3DRangeScan::unproject_LUT_t>
LUTs;
static std::mutex LUTs_mtx;
namespace
{
struct LUT_Storage
{
static LUT_Storage& Instance()
{
static LUT_Storage lut;
return lut;
}
std::unordered_map<LUT_info, CObservation3DRangeScan::unproject_LUT_t> LUTs;
std::mutex LUTs_mtx;
};

} // namespace

const CObservation3DRangeScan::unproject_LUT_t&
CObservation3DRangeScan::get_unproj_lut() const
{
#if MRPT_HAS_OPENCV

LUT_Storage& ls = LUT_Storage::Instance();

// Access to, or create upon first usage:
LUT_info linfo;
linfo.calib = this->cameraParams;
linfo.sensorPose = this->sensorPose;
linfo.range_is_depth = this->range_is_depth;

LUTs_mtx.lock();
// Important: keep lock until we return since this function modifies its
// contents:
auto lck = mrpt::lockHelper(ls.LUTs_mtx);

// Protect against infinite memory growth: imagine sensorPose gets changed
// every time for a sweeping sensor, etc.
// Unlikely, but "just in case" (TM)
if (LUTs.size() > 100) LUTs.clear();

const unproject_LUT_t& ret = LUTs[linfo];
if (ls.LUTs.size() > 100) ls.LUTs.clear();

LUTs_mtx.unlock();
const unproject_LUT_t& ret = ls.LUTs[linfo];

ASSERT_EQUAL_(rangeImage.cols(), static_cast<int>(cameraParams.ncols));
ASSERT_EQUAL_(rangeImage.rows(), static_cast<int>(cameraParams.nrows));
Expand Down

0 comments on commit 74598e9

Please sign in to comment.