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 MpMcQueue #483

Merged
merged 1 commit into from
Jun 30, 2024
Merged

Conversation

sosthene-nitrokey
Copy link
Contributor

Fix #482

impl<T, const N: usize> Drop for MpMcQueue<T, N> {
fn drop(&mut self) {
// drop all contents currently in the queue
while self.dequeue().is_some() {}
Copy link
Member

Choose a reason for hiding this comment

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

This is copying the elements to the stack before dropping them, and running the complex dequeue() logic.

it'd be more efficient to read enqueue_pos/dequeue_pos, iterate, then .assume_init_drop() all the items in place. No copies, no atomic ops. This is sound because on drop we have &mut self so we know no other thread can be concurrently accessing the queue at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to implement that but I'm not 100% certain I understand the layout of the Queue which is not documented, and which items are actually valid. I'm certain that the dequeue approach is correct so this is the approach I took.

Copy link
Member

Choose a reason for hiding this comment

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

ah yes, it's true it's not that trivial. We can optimize later if someone complains.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's open an Issue for that

Copy link
Member

@Dirbaio Dirbaio left a comment

Choose a reason for hiding this comment

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

thank you!

@Dirbaio Dirbaio added this pull request to the merge queue Jun 30, 2024
Merged via the queue into rust-embedded:main with commit 662941d Jun 30, 2024
22 checks passed
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.

mpmc: dropping a queue does not drop its content, which can cause a memory leak
2 participants