Skip to content

Commit a7c9a75

Browse files
SuperpifferOceania2018
authored andcommitted
Use a local Status variable
Using a local reference ensure that the Status object cannot be disposed before the Dispose. This way it's also possible to use an external Status instance instead of the static one, if needed.
1 parent 0ee50d3 commit a7c9a75

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

src/TensorFlowNET.Core/Sessions/BaseSession.cs

+9-20
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace Tensorflow
3030
public class BaseSession : DisposableObject
3131
{
3232
protected Graph _graph;
33+
protected Status _status;
3334
public Graph graph => _graph;
3435

3536
public BaseSession(IntPtr handle, Graph g)
@@ -48,9 +49,9 @@ public BaseSession(string target = "", Graph g = null, ConfigProto config = null
4849
}
4950

5051
using var opts = new SessionOptions(target, config);
51-
status = status ?? tf.Status;
52-
_handle = c_api.TF_NewSession(_graph, opts.Handle, status.Handle);
53-
status.Check(true);
52+
_status = status ?? tf.Status;
53+
_handle = c_api.TF_NewSession(_graph, opts.Handle, _status.Handle);
54+
_status.Check(true);
5455
}
5556

5657
public virtual void run(Operation op, params FeedItem[] feed_dict)
@@ -217,8 +218,6 @@ private unsafe NDArray[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] f
217218
// Ensure any changes to the graph are reflected in the runtime.
218219
_extend_graph();
219220

220-
var status = tf.Status;
221-
222221
var output_values = fetch_list.Select(x => IntPtr.Zero).ToArray();
223222

224223
c_api.TF_SessionRun(_handle,
@@ -232,9 +231,9 @@ private unsafe NDArray[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] f
232231
target_opers: target_list.Select(f => (IntPtr)f).ToArray(),
233232
ntargets: target_list.Count,
234233
run_metadata: IntPtr.Zero,
235-
status: status.Handle);
234+
status: _status.Handle);
236235

237-
status.Check(true);
236+
_status.Check(true);
238237

239238
var result = new NDArray[fetch_list.Length];
240239

@@ -246,8 +245,6 @@ private unsafe NDArray[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] f
246245

247246
public unsafe Tensor eval(Tensor tensor)
248247
{
249-
var status = tf.Status;
250-
251248
var output_values = new IntPtr[1];
252249
var fetch_list = new[] { tensor._as_tf_output() };
253250

@@ -262,9 +259,9 @@ public unsafe Tensor eval(Tensor tensor)
262259
target_opers: new IntPtr[0],
263260
ntargets: 0,
264261
run_metadata: IntPtr.Zero,
265-
status: status.Handle);
262+
status: _status.Handle);
266263

267-
status.Check(true);
264+
_status.Check(true);
268265

269266
return new Tensor(new SafeTensorHandle(output_values[0]));
270267
}
@@ -291,15 +288,7 @@ private void _extend_graph()
291288
protected override void DisposeUnmanagedResources(IntPtr handle)
292289
{
293290
// c_api.TF_CloseSession(handle, tf.Status.Handle);
294-
if (tf.Status == null || tf.Status.Handle.IsInvalid)
295-
{
296-
using var status = new Status();
297-
c_api.TF_DeleteSession(handle, status.Handle);
298-
}
299-
else
300-
{
301-
c_api.TF_DeleteSession(handle, tf.Status.Handle);
302-
}
291+
c_api.TF_DeleteSession(handle, _status.Handle);
303292
}
304293
}
305294
}

0 commit comments

Comments
 (0)