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

Introduce Storage::forget() to fix memory leak in Matrix::generic_resize() #1379

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

nickmertin
Copy link

Fixes #1378

Adds a trait method Storage::forget() to clean up the storage while forgetting (i.e., not dropping) the contents. Makes use of this method instead of mem::forget() in the implementations of Reallocator for DefaultAllocator.

@Andlon
Copy link
Sponsor Collaborator

Andlon commented Apr 10, 2024

Thanks, this is great! I don't have the head space or time to wrap my head around the nuances of the Reallocator API here at the moment, but I wanted to ask if the documentation needs to be updated to reflect any possible changes in assumptions made. I don't fully understand them yet myself, I'd need a closer look later to try to verify.

@nickmertin
Copy link
Author

Thanks! I looked over the documentation and I don't think any changes are necessary, as it maintains the assumption that the elements of the matrix are not dropped. My only question would be whether it would make more sense to move the new method to RawStorage instead of Storage.

@Andlon
Copy link
Sponsor Collaborator

Andlon commented May 6, 2024

I haven't forgotten this PR, but I haven't yet been able to devote time to it. It came up on Discord though, and @sebcrozet suggested that the forget function needs to be unsafe, and maybe it should be called forget_elements or something along those lines.

I'll try to get back to this soon, but I suspect I'll be quite busy this week.

@nickmertin
Copy link
Author

I like the name forget_elements, I think it gets the behaviour across more accurately than what I put right now.

I don't think it should be unsafe, for the same reason that mem::forget isn't unsafe. Safety does not guarantee that destructors are called, so there is nothing unsafe about calling this function. Marking it as unsafe would push obligations of ensuring safety onto everywhere it is called, which could be hard to follow given that it is a trait method and there are several implementations. Perhaps the behaviour and safety attributes just need to be better documented, as is the case for mem::forget.

Copy link
Collaborator

@tpdickso tpdickso left a comment

Choose a reason for hiding this comment

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

This looks appropriate to me! And I agree, I think a name like forget_elements would be great to help highlight that it's the elements that are being forgotten and not the storage itself (which will be dropped normally absent any other intervention.)

Copy link
Sponsor Collaborator

@Andlon Andlon left a comment

Choose a reason for hiding this comment

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

You're absolutely right about the fact that forget doesn't have to be unsafe @nickmertin! I just have a few minor suggestions, then we can get this merged in time for the next release :)

@@ -149,6 +149,9 @@ pub unsafe trait Storage<T, R: Dim, C: Dim = U1>: RawStorage<T, R, C> {
fn clone_owned(&self) -> Owned<T, R, C>
where
DefaultAllocator: Allocator<T, R, C>;

/// Cleans up the storage without calling the destructors on the contained elements.
Copy link
Sponsor Collaborator

Choose a reason for hiding this comment

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

As discussed, perhaps change the name to forget_elements.

I think perhaps "cleans up" is a little vague. Perhaps "Drops the storage without ..." ?


#[inline]
fn forget(mut self) {
// Set length to 0 so element destructors are not called.
Copy link
Sponsor Collaborator

Choose a reason for hiding this comment

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

Perhaps add a "SAFETY" comment here and explain that .set_len(0) always satisfies the invariants of .set_len, but the contents are deliberately leaked

(same for other forget impl)

the "SAFETY comment" practice is a relatively recent thing, and much of nalgebra's codebase, to my knowledge, predates this convention. But I think we should try to stick with it going forward, whenever it makes sense!

@Andlon Andlon added the patch version Merging PR only requires patch version bump label May 9, 2024
@nickmertin
Copy link
Author

Sounds good, I will make the necessary changes this weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch version Merging PR only requires patch version bump
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak in generic_resize/reallocate_copy
3 participants