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

Define custom copy/move constructors/operators for SharedData and GlobalEvent #493

Merged
merged 1 commit into from
Apr 24, 2024

Conversation

bdutro
Copy link
Contributor

@bdutro bdutro commented Apr 23, 2024

I ran into a segfault when I tried to create an std::vector of SharedData. There were two underlying causes for this:

  1. When GlobalEvent is moved/copied, it doesn't update ev_handler_lifetime_ to point to the new instance of event_handler_. If the original GlobalEvent was later destroyed, the copy would segfault when it tried to fire its handler.
  2. When SharedData is moved/copied, its ev_update_ receives a copy of the handler in the original SharedData. If the original was later destroyed, the copy would segfault when it tried to perform its state update.

This PR defines copy/move constructors and assignment operators for both classes so that everything gets updated properly and the copies can continue to function after the originals are destroyed.

GlobalEvent
    - This fixes a segfault that can occur after a SharedData is copied
@bdutro bdutro requested a review from klingaard April 23, 2024 22:03
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the exception of the move constructor, can these not be = default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we have to explicitly initialize ev_handler_lifetime_ to point to the ev_handler_ inside the new instance in all of these cases. Otherwise, it will point to rhs.ev_handler_.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see the difference. Thanks!

@@ -65,6 +70,44 @@ namespace sparta
writePS(std::forward<U>(init_val));
}

SharedData(const SharedData& rhs) :
ev_update_(rhs.ev_update_),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why copy the ev_update_ if it's going to be reset?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need the other fields inside ev_update_ to get copied.

@klingaard
Copy link
Member

Good catch. This has been plaguing me for quite some time!

@bdutro bdutro merged commit 000826f into master Apr 24, 2024
8 checks passed
@bdutro bdutro deleted the dev/bdutro/fix-shareddata-segfaults branch April 24, 2024 15:12
github-actions bot pushed a commit that referenced this pull request Apr 24, 2024
…ta and (#493)

GlobalEvent
    - This fixes a segfault that can occur after a SharedData is copied 000826f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants