Skip to content

Commit

Permalink
fix: drop inner Values in ValueRef(Mut)
Browse files Browse the repository at this point in the history
Since the actual underlying data contained in a `Value` is behind an `Arc<ValueInner>` (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`.
  • Loading branch information
decahedron1 committed Oct 19, 2024
1 parent 5f6411a commit c1c736b
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{
any::Any,
fmt::{self, Debug},
marker::PhantomData,
mem::ManuallyDrop,
ops::{Deref, DerefMut},
ptr::NonNull,
sync::Arc
Expand Down Expand Up @@ -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<Value<Type>>,
inner: Value<Type>,
lifetime: PhantomData<&'v ()>
}

impl<'v, Type: ValueTypeMarker + ?Sized> ValueRef<'v, Type> {
pub(crate) fn new(inner: Value<Type>) -> 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
Expand All @@ -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> {
Expand All @@ -288,16 +284,13 @@ impl<Type: ValueTypeMarker + ?Sized> 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<Value<Type>>,
inner: Value<Type>,
lifetime: PhantomData<&'v ()>
}

impl<'v, Type: ValueTypeMarker + ?Sized> ValueRefMut<'v, Type> {
pub(crate) fn new(inner: Value<Type>) -> 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
Expand All @@ -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> {
Expand Down

0 comments on commit c1c736b

Please sign in to comment.