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 memory leak of Coordinates in unit tests #2306

Merged
merged 1 commit into from
May 13, 2021
Merged

Conversation

ZedThree
Copy link
Member

@ZedThree ZedThree commented May 6, 2021

The Coordinates' metric elements can hold shared_ptrs that result in a
cyclic dependency between the Fields and the Coordinates, with the
result that some unit tests have memory leaks.

One place the issue can arise is when multiplying fields by metric
elements with CHECK > 0. Then, ASSERT1_FIELDS_COMPATIBLE checks
that both fields share a Coordinates -- but this has the side effect
of initialising the shared_ptr in the metric elements.

This means that when the Mesh gets destroyed, the coords_map
starts to destroy its contents, but because the Coordinates metric
elements members have references to the Coordinates, the
Coordinates in coords_map doesn't get destroyed. The end result is
that the Coordinates gets leaked.

The fix here is to use a weak_ptr in Field. This only gets a
reference to the Coordinates when needed.

The Coordinates' metric elements can hold shared_ptrs that result in a
cyclic dependency between the Fields and the Coordinates, with the
result that some unit tests have memory leaks.

One place the issue can arise is when multiplying fields by metric
elements with `CHECK > 0`. Then, `ASSERT1_FIELDS_COMPATIBLE` checks
that both fields share a `Coordinates` -- but this has the side effect
of initialising the `shared_ptr` in the metric elements.

This means that when the `Mesh` gets destroyed, the `coords_map`
starts to destroy its contents, but because the `Coordinates` metric
elements members have references to the `Coordinates`, the
`Coordinates` in `coords_map` doesn't get destroyed. The end result is
that the `Coordinates` gets leaked.

The fix here is to use a `weak_ptr` in `Field`. This only gets a
reference to the `Coordinates` when needed.
@ZedThree ZedThree added the bugfix label May 6, 2021
@ZedThree ZedThree added this to the BOUT-5.0 milestone May 6, 2021
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

@@ -166,7 +166,7 @@ public:
}
protected:
Mesh* fieldmesh{nullptr};
mutable std::shared_ptr<Coordinates> fieldCoordinates{nullptr};
mutable std::weak_ptr<Coordinates> fieldCoordinates{};
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: member variable fieldCoordinates has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

mutable std::weak_ptr<Coordinates> fieldCoordinates{};
                                   ^

@ZedThree ZedThree merged commit 60d4768 into next May 13, 2021
@ZedThree ZedThree deleted the fix-field-coords-leak branch November 30, 2021 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant