Skip to content

Commit 17df4c1

Browse files
sharwellOceania2018
authored andcommitted
Use leases to ensure handles are not released before calling StringPiece
1 parent 263fbcf commit 17df4c1

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

‎src/TensorFlowNET.Core/Eager/EagerTensor.cs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
using NumSharp;
22
using System;
3-
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Text;
4+
using Tensorflow.Util;
65
using static Tensorflow.Binding;
76

87
namespace Tensorflow.Eager
98
{
109
public partial class EagerTensor : Tensor
1110
{
12-
public override string Device => c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(EagerTensorHandle, tf.status.Handle));
11+
public override string Device
12+
{
13+
get
14+
{
15+
using var _ = EagerTensorHandle.Lease();
16+
return c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(EagerTensorHandle, tf.status.Handle));
17+
}
18+
}
1319

1420
public override int rank => c_api.TFE_TensorHandleNumDims(EagerTensorHandle, tf.status.Handle);
1521

‎src/TensorFlowNET.Core/Tensors/EagerTensorV2.cs‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
using NumSharp;
22
using System;
3-
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Reflection;
6-
using System.Runtime.InteropServices;
7-
using System.Text;
84
using Tensorflow.Eager;
5+
using Tensorflow.Util;
96
using static Tensorflow.Binding;
107

118
namespace Tensorflow
129
{
1310
public class EagerTensorV2 : DisposableObject, ITensor
1411
{
1512
SafeTensorHandleHandle EagerTensorHandle;
16-
public string Device => c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(EagerTensorHandle, tf.status.Handle));
13+
public string Device
14+
{
15+
get
16+
{
17+
using var _ = EagerTensorHandle.Lease();
18+
return c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(EagerTensorHandle, tf.status.Handle));
19+
}
20+
}
1721

1822
public EagerTensorV2(IntPtr handle)
1923
{

0 commit comments

Comments
 (0)