-
Notifications
You must be signed in to change notification settings - Fork 59
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
Conversation
GlobalEvent - This fixes a segfault that can occur after a SharedData is copied
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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_
.
There was a problem hiding this comment.
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_), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Good catch. This has been plaguing me for quite some time! |
I ran into a segfault when I tried to create an
std::vector
ofSharedData
. There were two underlying causes for this:GlobalEvent
is moved/copied, it doesn't updateev_handler_lifetime_
to point to the new instance ofevent_handler_
. If the originalGlobalEvent
was later destroyed, the copy would segfault when it tried to fire its handler.SharedData
is moved/copied, itsev_update_
receives a copy of the handler in the originalSharedData
. 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.