-
Notifications
You must be signed in to change notification settings - Fork 90
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
Fixed issue when recursively creating Market objects #904
Fixed issue when recursively creating Market objects #904
Conversation
@@ -186,7 +186,7 @@ std::vector<types::energy::EnforcedLimits> EnergyManager::run_optimizer(types::e | |||
optimized_values.reserve(brokers.size()); | |||
|
|||
for (auto broker : brokers) { | |||
auto local_market = broker->get_local_market(); | |||
auto& local_market = broker->get_local_market(); |
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.
👍
modules/EnergyManager/Market.hpp
Outdated
@@ -77,7 +76,7 @@ class Market { | |||
|
|||
private: | |||
Market* _parent; | |||
std::vector<Market> _children; | |||
std::vector<std::unique_ptr<Market>> _children; |
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.
I'm not sure why you added std::unique_ptr
here - I would appreciate if you mention this in the commit message (what was the failure and how it got solved).
I guess, the Market
objects don't behave well, when the vector gets enhanced or references are taken to these elements and become dangling when the vector gets resized. If this is the case, a simpler solution would be to use std::list
instead of std::vector
in combination with std::unique_ptr
. std::list
also guarantees, that it's elements stay at the same address, when other elements get added or removed.
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.
Thanks, changed it to std::list
and added the reasoning in the PR description
…of children to unique_ptr Signed-off-by: Piet Gömpel <[email protected]>
* switched from std::vector to std::list type for Markets Signed-off-by: Piet Gömpel <[email protected]>
a97c521
to
827e50f
Compare
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.
💯
Issue
The Market class previously used a
std::vector<Market> _children
to store childMarket
objects, which were initialized recursively within theMarket
constructor:The problem with that is that
std::vector
may need to resize and move its elements to a new memory location when additional elements are added. When this happens, all existingMarket
elements are moved, which invalidates any pointers or references to those elements. In this case, the_parent
pointer in could end up pointing to an invalid memory location, leading to segmentation faults when accessed.Changes
_children
fromstd::vector
tostd::list
, as it guarantees that its elements remain at stable memory addresses even when new elements are inserted. This ensures that_parent
pointers remain valid and point to the correct objectschildren()
function because it was dead code and no longer needed.Issue ticket number and link
Checklist before requesting a review