From 17df4c1c4441992cb84a0b1e6b87568f18e4b598 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 6 Jul 2020 08:20:37 -0700 Subject: [PATCH] Use leases to ensure handles are not released before calling StringPiece --- src/TensorFlowNET.Core/Eager/EagerTensor.cs | 12 +++++++++--- src/TensorFlowNET.Core/Tensors/EagerTensorV2.cs | 14 +++++++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/TensorFlowNET.Core/Eager/EagerTensor.cs b/src/TensorFlowNET.Core/Eager/EagerTensor.cs index 5761ce6e..bddd8188 100644 --- a/src/TensorFlowNET.Core/Eager/EagerTensor.cs +++ b/src/TensorFlowNET.Core/Eager/EagerTensor.cs @@ -1,15 +1,21 @@ using NumSharp; using System; -using System.Collections.Generic; using System.Linq; -using System.Text; +using Tensorflow.Util; using static Tensorflow.Binding; namespace Tensorflow.Eager { public partial class EagerTensor : Tensor { - public override string Device => c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(EagerTensorHandle, tf.status.Handle)); + public override string Device + { + get + { + using var _ = EagerTensorHandle.Lease(); + return c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(EagerTensorHandle, tf.status.Handle)); + } + } public override int rank => c_api.TFE_TensorHandleNumDims(EagerTensorHandle, tf.status.Handle); diff --git a/src/TensorFlowNET.Core/Tensors/EagerTensorV2.cs b/src/TensorFlowNET.Core/Tensors/EagerTensorV2.cs index b2ba7f28..db6bcb85 100644 --- a/src/TensorFlowNET.Core/Tensors/EagerTensorV2.cs +++ b/src/TensorFlowNET.Core/Tensors/EagerTensorV2.cs @@ -1,11 +1,8 @@ using NumSharp; using System; -using System.Collections.Generic; using System.Linq; -using System.Reflection; -using System.Runtime.InteropServices; -using System.Text; using Tensorflow.Eager; +using Tensorflow.Util; using static Tensorflow.Binding; namespace Tensorflow @@ -13,7 +10,14 @@ namespace Tensorflow public class EagerTensorV2 : DisposableObject, ITensor { SafeTensorHandleHandle EagerTensorHandle; - public string Device => c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(EagerTensorHandle, tf.status.Handle)); + public string Device + { + get + { + using var _ = EagerTensorHandle.Lease(); + return c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(EagerTensorHandle, tf.status.Handle)); + } + } public EagerTensorV2(IntPtr handle) {