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 a memory leak in AlignedVector. #13005

Merged
merged 2 commits into from Nov 30, 2021
Merged

Fix a memory leak in AlignedVector. #13005

merged 2 commits into from Nov 30, 2021

Conversation

bangerth
Copy link
Member

Fixes #12998. As pointed out in #12993.

/rebuild

resize(0);
resize_fast(vec.used_elements_end - vec.elements.get());
reserve(new_size);
Copy link
Member Author

Choose a reason for hiding this comment

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

The bug is here: When we call resize_fast(), we allocate memory and default construct objects in that memory. But in the line below (AlignedVectorCopyConstruct) we copy-construct other objects in that same memory location, forgetting to call the destructor on what we had done before.

The solution is to not initialize the memory we allocate -- that's what reserve() does in contrast to resize*().

Copy link
Member Author

Choose a reason for hiding this comment

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

For reference, on second look, I see that in essence the same code I just wrote here is also what's in the copy constructor just a few lines up.

@bangerth
Copy link
Member Author

@elauksap FYI.

@pcafrica
Copy link
Contributor

@elauksap FYI.

That's brilliant, thanks a lot!

@peterrum
Copy link
Member

@elauksap Could you try out this patch without #12995.

@pcafrica
Copy link
Contributor

@peterrum just tried and it works! You can safely revert my patch, if needed.

@peterrum peterrum merged commit ae407cf into dealii:master Nov 30, 2021
@kronbichler
Copy link
Member

I would vote for keeping the patch, it is a nice use of compile-time knowledge and hence more direct in my opinion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak Table/AlignedVector
5 participants