From c1c736bad8b647ef586321554b933f3482ee46cf Mon Sep 17 00:00:00 2001 From: "Carson M." Date: Sat, 19 Oct 2024 11:51:29 -0500 Subject: [PATCH] fix: drop inner `Value`s in `ValueRef(Mut)` Since the actual underlying data contained in a `Value` is behind an `Arc` (which is cloned to create refs), not dropping the newly created `Value` would at best not decrease the `ValueInner`'s strong count, and at worse never free `OrtValue`s created via `Tensor::from_raw`. --- src/value/mod.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/value/mod.rs b/src/value/mod.rs index 2066ff6..64b4053 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -2,7 +2,6 @@ use std::{ any::Any, fmt::{self, Debug}, marker::PhantomData, - mem::ManuallyDrop, ops::{Deref, DerefMut}, ptr::NonNull, sync::Arc @@ -234,16 +233,13 @@ impl ValueInner { /// A temporary version of a [`Value`] with a lifetime specifier. #[derive(Debug)] pub struct ValueRef<'v, Type: ValueTypeMarker + ?Sized = DynValueTypeMarker> { - inner: ManuallyDrop>, + inner: Value, lifetime: PhantomData<&'v ()> } impl<'v, Type: ValueTypeMarker + ?Sized> ValueRef<'v, Type> { pub(crate) fn new(inner: Value) -> Self { - ValueRef { - inner: ManuallyDrop::new(inner), - lifetime: PhantomData - } + ValueRef { inner, lifetime: PhantomData } } /// Attempts to downcast a temporary dynamic value (like [`DynValue`] or [`DynTensor`]) to a more strongly typed @@ -269,7 +265,7 @@ impl<'v, Type: ValueTypeMarker + ?Sized> ValueRef<'v, Type> { return Err(self); } - Ok(ManuallyDrop::into_inner(self.inner)) + Ok(self.inner) } pub fn into_dyn(self) -> ValueRef<'v, DynValueTypeMarker> { @@ -288,16 +284,13 @@ impl Deref for ValueRef<'_, Type> { /// A mutable temporary version of a [`Value`] with a lifetime specifier. #[derive(Debug)] pub struct ValueRefMut<'v, Type: ValueTypeMarker + ?Sized = DynValueTypeMarker> { - inner: ManuallyDrop>, + inner: Value, lifetime: PhantomData<&'v ()> } impl<'v, Type: ValueTypeMarker + ?Sized> ValueRefMut<'v, Type> { pub(crate) fn new(inner: Value) -> Self { - ValueRefMut { - inner: ManuallyDrop::new(inner), - lifetime: PhantomData - } + ValueRefMut { inner, lifetime: PhantomData } } /// Attempts to downcast a temporary mutable dynamic value (like [`DynValue`] or [`DynTensor`]) to a more @@ -323,7 +316,7 @@ impl<'v, Type: ValueTypeMarker + ?Sized> ValueRefMut<'v, Type> { return Err(self); } - Ok(ManuallyDrop::into_inner(self.inner)) + Ok(self.inner) } pub fn into_dyn(self) -> ValueRefMut<'v, DynValueTypeMarker> {