Skip to content

Commit

Permalink
Merge pull request #306 from pritzvac/mutex_state_bugfix
Browse files Browse the repository at this point in the history
Fix: Segmentation fault in State.h
  • Loading branch information
goldbattle committed Mar 15, 2023
2 parents 98c5525 + c5477af commit 7fe8f58
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ov_msckf/src/state/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class State {
* @return timestep of clone we will marginalize
*/
double margtimestep() {
std::lock_guard<std::mutex> lock(_mutex_state);
double time = INFINITY;
for (const auto &clone_imu : _clones_IMU) {
if (clone_imu.first < time) {
Expand All @@ -78,6 +79,9 @@ class State {
*/
int max_covariance_size() { return (int)_Cov.rows(); }

/// Mutex for locking access to the state
std::mutex _mutex_state;

/// Current timestamp (should be the last update time!)
double _timestamp = -1;

Expand Down
2 changes: 2 additions & 0 deletions ov_msckf/src/state/StateHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ void StateHelper::augment_clone(std::shared_ptr<State> state, Eigen::Matrix<doub
void StateHelper::marginalize_old_clone(std::shared_ptr<State> state) {
if ((int)state->_clones_IMU.size() > state->_options.max_clone_size) {
double marginal_time = state->margtimestep();
// Lock the mutex to avoid deleting any elements from _clones_IMU while accessing it from other threads
std::lock_guard<std::mutex> lock(state->_mutex_state);
assert(marginal_time != INFINITY);
StateHelper::marginalize(state, state->_clones_IMU.at(marginal_time));
// Note that the marginalizer should have already deleted the clone
Expand Down

0 comments on commit 7fe8f58

Please sign in to comment.