Skip to content

Commit 33d82e8

Browse files
committed
GC.SuppressFinalize for TF object.
1 parent a8aac8d commit 33d82e8

9 files changed

Lines changed: 801 additions & 668 deletions

File tree

src/TensorFlowNET.Core/Graphs/Graph.Export.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public Buffer ToGraphDef(Status s)
3131

3232
private GraphDef _as_graph_def(bool add_shapes = false)
3333
{
34-
var buffer = ToGraphDef(Status);
35-
Status.Check();
34+
var status = new Status();
35+
var buffer = ToGraphDef(status);
36+
status.Check();
3637
var def = GraphDef.Parser.ParseFrom(buffer);
3738
buffer.Dispose();
3839

src/TensorFlowNET.Core/Graphs/Graph.Import.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,20 @@ public Status Import(string file_path)
4343
var bytes = File.ReadAllBytes(file_path);
4444
var graph_def = new Tensorflow.Buffer(bytes);
4545
var opts = c_api.TF_NewImportGraphDefOptions();
46-
c_api.TF_GraphImportGraphDef(_handle, graph_def, opts, Status);
47-
return Status;
46+
var status = new Status();
47+
c_api.TF_GraphImportGraphDef(_handle, graph_def, opts, status);
48+
return status;
4849
}
4950

50-
public Status Import(byte[] bytes)
51+
public Status Import(byte[] bytes, string prefix = "")
5152
{
5253
var graph_def = new Tensorflow.Buffer(bytes);
5354
var opts = c_api.TF_NewImportGraphDefOptions();
54-
c_api.TF_GraphImportGraphDef(_handle, graph_def, opts, Status);
55-
return Status;
55+
c_api.TF_ImportGraphDefOptionsSetPrefix(opts, prefix);
56+
var status = new Status();
57+
c_api.TF_GraphImportGraphDef(_handle, graph_def, opts, status);
58+
c_api.TF_DeleteImportGraphDefOptions(opts);
59+
return status;
5660
}
5761

5862
public static Graph ImportFromPB(string file_path, string name = null)

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ public partial class Graph : IPython, IDisposable, IEnumerable<Operation>
8888
private string _graph_key;
8989
public string graph_key => _graph_key;
9090
public string _last_loss_reduction;
91-
public bool _is_loss_scaled_by_optimizer { get; set; }
92-
public Status Status { get; }
91+
public bool _is_loss_scaled_by_optimizer { get; set; }
9392

9493
/// <summary>
9594
/// True if the graph is considered "finalized". In that case no
@@ -107,7 +106,6 @@ public partial class Graph : IPython, IDisposable, IEnumerable<Operation>
107106
public Graph()
108107
{
109108
_handle = c_api.TF_NewGraph();
110-
Status = new Status();
111109
_nodes_by_id = new Dictionary<int, ITensorOrOperation>();
112110
_nodes_by_name = new Dictionary<string, ITensorOrOperation>();
113111
_names_in_use = new Dictionary<string, int>();
@@ -117,7 +115,6 @@ public Graph()
117115
public Graph(IntPtr handle)
118116
{
119117
_handle = handle;
120-
Status = new Status();
121118
_nodes_by_id = new Dictionary<int, ITensorOrOperation>();
122119
_nodes_by_name = new Dictionary<string, ITensorOrOperation>();
123120
_names_in_use = new Dictionary<string, int>();
@@ -448,7 +445,12 @@ public void prevent_fetching(Operation op)
448445

449446
public void Dispose()
450447
{
451-
// c_api.TF_DeleteGraph(_handle);
448+
if (_handle != IntPtr.Zero)
449+
c_api.TF_DeleteGraph(_handle);
450+
451+
_handle = IntPtr.Zero;
452+
453+
GC.SuppressFinalize(this);
452454
}
453455

454456
/// <summary>

src/TensorFlowNET.Core/Sessions/BaseSession.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,19 @@ public class BaseSession
3232
protected int _current_version;
3333
protected byte[] _target;
3434
protected IntPtr _session;
35-
public Status Status;
3635
public Graph graph => _graph;
3736

3837
public BaseSession(string target = "", Graph g = null, SessionOptions opts = null)
3938
{
4039
_graph = g is null ? ops.get_default_graph() : g;
41-
40+
_graph.as_default();
4241
_target = UTF8Encoding.UTF8.GetBytes(target);
4342

4443
SessionOptions newOpts = null;
4544
if (opts == null)
4645
newOpts = c_api.TF_NewSessionOptions();
4746

48-
Status = new Status();
47+
var Status = new Status();
4948

5049
_session = c_api.TF_NewSession(_graph, opts ?? newOpts, Status);
5150

src/TensorFlowNET.Core/Sessions/Session.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Session(Graph g, SessionOptions opts = null, Status s = null)
3737
: base("", g, opts)
3838
{
3939
if (s == null)
40-
s = Status;
40+
s = new Status();
4141
}
4242

4343
public Session as_default()
@@ -83,8 +83,19 @@ public void close()
8383

8484
public void Dispose()
8585
{
86-
c_api.TF_DeleteSession(_session, Status);
87-
Status.Dispose();
86+
if (_session != IntPtr.Zero)
87+
{
88+
var status = new Status();
89+
c_api.TF_DeleteSession(_session, status);
90+
}
91+
92+
_session = IntPtr.Zero;
93+
GC.SuppressFinalize(this);
94+
}
95+
96+
~Session()
97+
{
98+
Dispose();
8899
}
89100

90101
public void __enter__()

src/TensorFlowNET.Core/TensorFlowNET.Core.csproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<AssemblyName>TensorFlow.NET</AssemblyName>
66
<RootNamespace>Tensorflow</RootNamespace>
77
<TargetTensorFlow>1.14.0</TargetTensorFlow>
8-
<Version>0.10.4</Version>
8+
<Version>0.10.7</Version>
99
<Authors>Haiping Chen, Meinrad Recheis</Authors>
1010
<Company>SciSharp STACK</Company>
1111
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
@@ -17,7 +17,7 @@
1717
<PackageTags>TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C#</PackageTags>
1818
<Description>Google's TensorFlow full binding in .NET Standard.
1919
Docs: https://tensorflownet.readthedocs.io</Description>
20-
<AssemblyVersion>0.10.4.0</AssemblyVersion>
20+
<AssemblyVersion>0.10.7.0</AssemblyVersion>
2121
<PackageReleaseNotes>Changes since v0.9.0:
2222

2323
1. Added full connected Convolution Neural Network example.
@@ -31,9 +31,12 @@ Docs: https://tensorflownet.readthedocs.io</Description>
3131
9. Fix strided_slice_grad type convention error.
3232
10. Add AbsGrad.
3333
11. Fix Session.LoadFromSavedModel(string).
34-
12. Add Tensor operator overloads.</PackageReleaseNotes>
34+
12. Add Tensor operator overloads.
35+
13. Fix default graph and operation issue when import model.
36+
14. Fix TF_String endcode and decode.
37+
15. Fix Tensor memory leak.</PackageReleaseNotes>
3538
<LangVersion>7.2</LangVersion>
36-
<FileVersion>0.10.4.0</FileVersion>
39+
<FileVersion>0.10.7.0</FileVersion>
3740
<PackageLicenseFile>LICENSE</PackageLicenseFile>
3841
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
3942
<SignAssembly>true</SignAssembly>

0 commit comments

Comments
 (0)