constellation icon indicating copy to clipboard operation
constellation copied to clipboard

VecResource<T> must implement Drop

Open Wyverex42 opened this issue 7 years ago • 0 comments

In its current implementation, VecResource<T> 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);
    }
}

Wyverex42 avatar Feb 28 '17 14:02 Wyverex42