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

Investigate Skipmap memory leaks #12

Closed
dermesser opened this issue Mar 6, 2022 · 1 comment
Closed

Investigate Skipmap memory leaks #12

dermesser opened this issue Mar 6, 2022 · 1 comment
Labels

Comments

@dermesser
Copy link
Owner

There seems to be a bug in the implementation of SkipMap leading to memory leaks. I found this while investigating #11 which refers to a different area though.

Instrumenting the Drop implementation:

impl Drop for Node {
    fn drop(&mut self) {
        // large object should drop
        if let Some(mut next) = self.next.take() {
            println!("destroyed {:?}", next.key);
            while let Some(child) = next.next.take() {
                next = child;
                println!("destroyed {:?}", next.key);
            }
        }
            println!("destroyed {:?}", self.key);
        unsafe {
            for skip in self.skips.iter_mut() {
                if let Some(mut next) = skip.take() {
                    while let Some(child) = (*next).next.take() {
                        next = Box::into_raw(child);
                    }
                }
            }
        }
    }
}

gives

running 1 test
destroyed [97, 98, 97]
destroyed [97, 98, 97]
destroyed [97, 98, 98]
destroyed [97, 98, 98]
destroyed []
test skipmap::tests::test_skipmap_iterator_init ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 142 filtered out; finished in 0.00s

for a skipmap constructed as

    pub fn make_skipmap() -> SkipMap {
        let mut skm = SkipMap::new(options::for_test().cmp);
        let keys = vec![
            "aba", "abb", "abc", "abd", "abe", "abf", "abg", "abh", "abi", "abj", "abk", "abl",
            "abm", "abn", "abo", "abp", "abq", "abr", "abs", "abt", "abu", "abv", "abw", "abx",
            "aby", "abz",
        ];

        for k in keys {
            skm.insert(k.as_bytes().to_vec(), "def".as_bytes().to_vec());
        }
        skm
    }
@dermesser
Copy link
Owner Author

Nicely enough, the default implementation already did the right thing. By deleting the Drop impl, this memory leak is gone.

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

No branches or pull requests

1 participant