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

Memory leak because of missing Drop implementation. #79

Closed
cafce25 opened this issue Apr 25, 2023 · 7 comments
Closed

Memory leak because of missing Drop implementation. #79

cafce25 opened this issue Apr 25, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@cafce25
Copy link

cafce25 commented Apr 25, 2023

Currently when a Map gets dropped it does not clean up any of it's keys or values i.e. the following tests fail:

#[test]
fn drops_keys() {
    use std::rc::Rc;
    let mut m: Map<Rc<()>, (), 8> = Map::new();
    let k = Rc::new(());
    m.insert(Rc::clone(&k), ());
    drop(m);
    assert_eq!(Rc::strong_count(&k), 1);
}

#[test]
fn drops_values() {
    use std::rc::Rc;
    let mut m: Map<(), Rc<()>, 8> = Map::new();
    let v = Rc::new(());
    m.insert((), Rc::clone(&v));
    drop(m);
    assert_eq!(Rc::strong_count(&v), 1);
}
@yegor256
Copy link
Owner

@cafce25 many thanks! How would you recommend implementing it? I'm trying this, but doesn't help:

impl<K: Clone + PartialEq, V: Clone, const N: usize> Drop for Map<K, V, N> {
    fn drop(&mut self) {
        for (k, v) in self.iter() {
            drop(k);
            drop(v);
        }
    }
}

@yegor256 yegor256 added the bug Something isn't working label Apr 25, 2023
yegor256 added a commit that referenced this issue Apr 25, 2023
@yegor256
Copy link
Owner

@cafce25 I added the tests: 4fb2bb0

@cafce25
Copy link
Author

cafce25 commented Apr 26, 2023

Probably some combination of iteration over self.pairs and MabeUninit::assume_init_drop() or add and use a IntoIterator implementation of Map should work.

yegor256 added a commit that referenced this issue Apr 26, 2023
@yegor256
Copy link
Owner

@cafce25 once again, thanks! Both of your tests work: 93f5097

@yegor256
Copy link
Owner

@rultor release, tag is 0.0.10

@rultor
Copy link
Collaborator

rultor commented Apr 26, 2023

@rultor release, tag is 0.0.10

@yegor256 OK, I will release it now. Please check the progress here

@rultor
Copy link
Collaborator

rultor commented Apr 26, 2023

@rultor release, tag is 0.0.10

@yegor256 Done! FYI, the full log is here (took me 3min)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants