From 0ceaf429f88a2e9fd2c7c88d1c07d72ace89e5d4 Mon Sep 17 00:00:00 2001 From: bluss Date: Sat, 18 Apr 2020 14:12:41 +0200 Subject: [PATCH] FIX: Skip dropping array elements that don't need drop While this sounds tautological; to be careful, set the vector length to zero before dropping the vector of the internal storage of an array. See the comment in the code for why tihs makes a difference. --- src/data_repr.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/data_repr.rs b/src/data_repr.rs index c7a8b96fa..ccaed85c0 100644 --- a/src/data_repr.rs +++ b/src/data_repr.rs @@ -86,6 +86,18 @@ impl Clone for OwnedRepr impl Drop for OwnedRepr { fn drop(&mut self) { if self.capacity > 0 { + // correct because: If the elements don't need dropping, an + // empty Vec is ok. Only the Vec's allocation needs dropping. + // + // implemented because: in some places in ndarray + // where A: Copy (hence does not need drop) we use uninitialized elements in + // vectors. Setting the length to 0 avoids that the vector tries to + // drop, slice or otherwise produce values of these elements. + // (The details of the validity letting this happen with nonzero len, are + // under discussion as of this writing.) + if !mem::needs_drop::() { + self.len = 0; + } // drop as a Vec. self.take_as_vec(); }