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

VecResource<T> must implement Drop #2

Open
Wyverex42 opened this issue Feb 28, 2017 · 0 comments
Open

VecResource<T> must implement Drop #2

Wyverex42 opened this issue Feb 28, 2017 · 0 comments

Comments

@Wyverex42
Copy link

In its current implementation, VecResource will drop all contained values when it's deleted, even the uninitialized ones. Depending on what T is, this might go unnoticed for a long time or simply crash (as in my recent case). The correct implementation should probably be something like this:

impl<T: Any + Send + Sync> Drop for VecResource<T> {
    fn drop(&mut self) {
        use std::mem::drop;

        for i in 0..self.storage.v.len() {
            if self.storage.g[i] != None {
                unsafe {
                    drop(self.storage.v.get_unchecked_mut(i));
                }
            }
        }
        unsafe {
            self.storage.v.set_len(0);
        }
        drop(&mut self.storage.v);
        drop(&mut self.storage.g);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant