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
base: dev
Are you sure you want to change the base?
Conversation
Thanks, this is great! I don't have the head space or time to wrap my head around the nuances of the |
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 |
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 I'll try to get back to this soon, but I suspect I'll be quite busy this week. |
I like the name I don't think it should be |
There was a problem hiding this 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.)
There was a problem hiding this 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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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!
Sounds good, I will make the necessary changes this weekend. |
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 ofmem::forget()
in the implementations ofReallocator
forDefaultAllocator
.