Skip to content

Commit 28e19ea

Browse files
sharwellEsther2013
authored andcommitted
Ensure SafeDeviceListHandle is not released before calling StringPiece
1 parent 028b977 commit 28e19ea

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/TensorFlowNET.Core/Device/c_api.device.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
using System.Runtime.InteropServices;
1919
using Tensorflow.Device;
2020
using Tensorflow.Eager;
21+
using Tensorflow.Util;
2122

2223
namespace Tensorflow
2324
{
@@ -68,6 +69,18 @@ public partial class c_api
6869
[DllImport(TensorFlowLibName)]
6970
public static extern IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, SafeContextHandle ctx, string device_name, SafeStatusHandle status);
7071

72+
/// <summary>
73+
/// Retrieves the full name of the device (e.g. /job:worker/replica:0/...)
74+
/// </summary>
75+
/// <param name="list">TF_DeviceList*</param>
76+
/// <param name="index"></param>
77+
/// <param name="status">TF_Status*</param>
78+
public static string TF_DeviceListName(SafeDeviceListHandle list, int index, SafeStatusHandle status)
79+
{
80+
using var _ = list.Lease();
81+
return StringPiece(TF_DeviceListNameImpl(list, index, status));
82+
}
83+
7184
/// <summary>
7285
/// Retrieves the full name of the device (e.g. /job:worker/replica:0/...)
7386
/// The return value will be a pointer to a null terminated string. The caller
@@ -77,7 +90,7 @@ public partial class c_api
7790
/// <param name="list">TF_DeviceList*</param>
7891
/// <param name="index"></param>
7992
/// <param name="status">TF_Status*</param>
80-
[DllImport(TensorFlowLibName)]
81-
public static extern IntPtr TF_DeviceListName(SafeDeviceListHandle list, int index, SafeStatusHandle status);
93+
[DllImport(TensorFlowLibName, EntryPoint = "TF_DeviceListName")]
94+
private static extern IntPtr TF_DeviceListNameImpl(SafeDeviceListHandle list, int index, SafeStatusHandle status);
8295
}
8396
}

test/TensorFlowNET.UnitTest/NativeAPI/CApiTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected string TF_DeviceListType(SafeDeviceListHandle list, int index, SafeSta
147147
=> c_api.StringPiece(c_api.TF_DeviceListType(list, index, status));
148148

149149
protected string TF_DeviceListName(SafeDeviceListHandle list, int index, SafeStatusHandle status)
150-
=> c_api.StringPiece(c_api.TF_DeviceListName(list, index, status));
150+
=> c_api.TF_DeviceListName(list, index, status);
151151

152152
protected IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, SafeContextHandle ctx, string device_name, SafeStatusHandle status)
153153
=> c_api.TFE_TensorHandleCopyToDevice(h, ctx, device_name, status);

0 commit comments

Comments
 (0)