From 97c891114d683037e031279e39aee274115e9a79 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Tue, 16 Feb 2021 13:02:32 +0800 Subject: [PATCH] fix(deque): correct layout size to dealloc --- src/deque/mod.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/deque/mod.rs b/src/deque/mod.rs index ac79f71a..ab574cd9 100644 --- a/src/deque/mod.rs +++ b/src/deque/mod.rs @@ -541,19 +541,14 @@ impl Drop for RawVec { /// Deallocates the underlying memory region by calculating the type layout /// and number of elements. /// - /// This method only deallocates when containing actual sized elements. - /// - /// Note that this only drop the memory block allocated by `RawVec` itself - /// without dropping the contents. Callers may need to drop the contents - /// by themselves. + /// This only drop the memory block allocated by `RawVec` itself but not + /// dropping the contents. Callers need to drop the contents by themselves. fn drop(&mut self) { - let size = mem::size_of::() * self.cap; - if size > 0 { - let align = mem::align_of::(); - let layout = Layout::from_size_align(size, align).unwrap(); + let layout = Layout::array::(self.cap).unwrap(); // 1 + if layout.size() > 0 { // This is safe because it conforms to the [safety contracts][1]. // - // [1] https://doc.rust-lang.org/1.49.0/alloc/alloc/trait.GlobalAlloc.html#safety-2 + // [1]: https://doc.rust-lang.org/1.49.0/alloc/alloc/trait.GlobalAlloc.html#safety-2 unsafe { dealloc(self.ptr.cast(), layout) } } }