Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Run destructors when clearing PinVec #21

Merged
merged 4 commits into from
Apr 26, 2022
Merged

Conversation

eholk
Copy link
Collaborator

@eholk eholk commented Apr 25, 2022

Also adds a Drop impl for PinVec, and removes the Drop impl for PinSlab since PinSlab's Drop is redundant now.

Fixes #20, and also includes the test from #19.

udoprog and others added 3 commits April 25, 2022 11:40
Also adds a Drop impl for PinVec, and removes the Drop impl for PinSlab
since PinSlab's Drop is redundant now.

Fixes udoprog#20
src/pin_vec.rs Show resolved Hide resolved
@udoprog
Copy link
Owner

udoprog commented Apr 26, 2022

You can merge as-is or you can apply the suggestion. Your choice!

@eholk
Copy link
Collaborator Author

eholk commented Apr 26, 2022

Thanks for adding me to the repo! I'll go ahead and merge this with the change you mentioned. Please take a look though and I'm happy to make additional changes if needed.

@eholk eholk merged commit 259901f into udoprog:main Apr 26, 2022
@eholk eholk deleted the pin-slab-memory-leak branch April 26, 2022 21:51
// will not be accessed again.
unsafe { drop_in_place(self.get_mut(i).unwrap()) }
if mem::needs_drop::<T>() {
let (last_slot, offset, _) = calculate_key(self.len());
Copy link
Owner

Choose a reason for hiding this comment

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

Oh, this is really neat!

// Safety: we initialized slice to only point to the already-initialized elements.
// It's safe to drop_in_place because we are draining the Vec.
unsafe {
let slice: &mut [T] = mem::transmute(slice);
Copy link
Owner

Choose a reason for hiding this comment

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

I think I'm partial to casting to a pointer above and reconstituting it as a slice over transmuting a slice. But both are equally correct to me so I don't mind it.

Here's that version:

let len = if i < last_slot { slot.len() } else { offset };
drop_in_place(from_raw_parts_mut(slot.as_mut_ptr() as *mut T, len));

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PinSlab and Unordered leaks memory for non-Copy types
2 participants