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

Fixed issue when recursively creating Market objects #904

Merged
merged 3 commits into from
Oct 15, 2024

Conversation

Pietfried
Copy link
Contributor

@Pietfried Pietfried commented Oct 11, 2024

Issue

The Market class previously used a std::vector<Market> _children to store child Market objects, which were initialized recursively within the Market constructor:

for (auto& flow_child : _energy_flow_request.children) {
    _children.emplace_back(flow_child, _nominal_ac_voltage, this); // third argument is _parent of the object
}

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 existing Market 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

  • Switched _children from std::vector to std::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 objects
  • Removed the children() function because it was dead code and no longer needed.

Issue ticket number and link

Checklist before requesting a review

  • I have performed a self-review of my code
  • I have made corresponding changes to the documentation
  • I read the contribution documentation and made sure that my changes meet its requirements

@@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -77,7 +76,7 @@ class Market {

private:
Market* _parent;
std::vector<Market> _children;
std::vector<std::unique_ptr<Market>> _children;
Copy link
Contributor

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.

Copy link
Contributor Author

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]>
@Pietfried Pietfried force-pushed the fix/energymanager-multiple-market-children branch from a97c521 to 827e50f Compare October 14, 2024 16:08
@Pietfried Pietfried requested a review from a-w50 October 14, 2024 16:10
@Pietfried Pietfried changed the title Fixed issue when recursively creating Market objects. Changed vector … Fixed issue when recursively creating Market objects Oct 14, 2024
Copy link
Contributor

@a-w50 a-w50 left a comment

Choose a reason for hiding this comment

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

💯

@corneliusclaussen corneliusclaussen merged commit dce296f into main Oct 15, 2024
9 of 10 checks passed
@corneliusclaussen corneliusclaussen deleted the fix/energymanager-multiple-market-children branch October 15, 2024 06:36
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.

3 participants