Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Segmentation fault in State.h #306

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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