Skip to content

Commit 891346e

Browse files
committed
Refactored DisposableObject
1 parent 8546b8d commit 891346e

11 files changed

Lines changed: 44 additions & 42 deletions

File tree

src/TensorFlowNET.Core/APIs/c_api.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ public struct DeallocatorArgs
5959
}
6060

6161
[DllImport(TensorFlowLibName)]
62-
public static unsafe extern IntPtr TF_Version();
62+
public static extern IntPtr TF_Version();
6363
}
6464
}

src/TensorFlowNET.Core/Binding.Util.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,14 @@ public static __object__ getattr(object obj, string key, params Type[] ___parame
308308
public static IEnumerable TupleToEnumerable(object tuple)
309309
{
310310
Type t = tuple.GetType();
311-
if(t.IsGenericType && (t.FullName.StartsWith("System.Tuple") || t.FullName.StartsWith("System.ValueTuple")))
311+
if (t.IsGenericType && (t.FullName.StartsWith("System.Tuple") || t.FullName.StartsWith("System.ValueTuple")))
312312
{
313313
var flds = t.GetFields();
314-
for(int i = 0; i < flds.Length;i++)
314+
for (int i = 0; i < flds.Length; i++)
315315
{
316316
yield return flds[i].GetValue(tuple);
317317
}
318-
}
319-
else
318+
} else
320319
{
321320
throw new System.Exception("Expected Tuple.");
322321
}
@@ -329,12 +328,9 @@ public static bool isinstance(object Item1, Type Item2)
329328

330329
public static bool isinstance(object Item1, object tuple)
331330
{
332-
var tup = TupleToEnumerable(tuple);
333-
foreach(var t in tup)
334-
{
335-
if(isinstance(Item1, (Type)t))
331+
foreach (var t in TupleToEnumerable(tuple))
332+
if (isinstance(Item1, (Type) t))
336333
return true;
337-
}
338334
return false;
339335
}
340336
}

src/TensorFlowNET.Core/Buffers/Buffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static implicit operator byte[](Buffer buffer)
6666
return buffer.Data;
6767
}
6868

69-
protected override void DisposeUnManagedState(IntPtr handle)
69+
protected override void DisposeUnmanagedResources(IntPtr handle)
7070
=> c_api.TF_DeleteBuffer(handle);
7171
}
7272
}

src/TensorFlowNET.Core/DisposableObject.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,54 @@ public abstract class DisposableObject : IDisposable
2929

3030
protected DisposableObject() { }
3131

32-
public DisposableObject(IntPtr handle)
33-
{
34-
_handle = handle;
35-
}
36-
37-
protected virtual void DisposeManagedState()
38-
{
39-
}
40-
41-
protected abstract void DisposeUnManagedState(IntPtr handle);
32+
protected DisposableObject(IntPtr handle)
33+
=> _handle = handle;
4234

43-
protected virtual void Dispose(bool disposing)
35+
private void internal_dispose(bool disposing)
4436
{
4537
if (disposing)
4638
{
39+
// dispose managed state (managed objects).
40+
DisposeManagedResources();
41+
4742
// free unmanaged resources (unmanaged objects) and override a finalizer below.
4843
if (_handle != IntPtr.Zero)
4944
{
50-
// dispose managed state (managed objects).
51-
DisposeManagedState();
52-
5345
// set large fields to null.
54-
DisposeUnManagedState(_handle);
46+
DisposeUnmanagedResources(_handle);
5547

5648
_handle = IntPtr.Zero;
5749
}
5850
}
5951
}
6052

53+
/// <summary>
54+
/// Dispose any managed resources.
55+
/// </summary>
56+
/// <remarks>Equivalent to what you would perform inside <see cref="Dispose()"/></remarks>
57+
protected virtual void DisposeManagedResources()
58+
{
59+
}
60+
61+
/// <summary>
62+
/// Dispose any unmanaged resources related to given <paramref name="handle"/>.
63+
/// </summary>
64+
protected abstract void DisposeUnmanagedResources(IntPtr handle);
65+
6166
// override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
6267
~DisposableObject()
6368
{
6469
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
65-
Dispose(false);
70+
internal_dispose(false);
6671
}
6772

6873
// This code added to correctly implement the disposable pattern.
6974
public void Dispose()
7075
{
7176
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
72-
Dispose(true);
77+
internal_dispose(true);
7378
// uncomment the following line if the finalizer is overridden above.
7479
GC.SuppressFinalize(this);
7580
}
7681
}
77-
}
82+
}

src/TensorFlowNET.Core/Eager/ContextOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
2+
using System.IO;
23

34
namespace Tensorflow.Eager
45
{
5-
public class ContextOptions : IDisposable
6+
public class ContextOptions : IDisposable //TODO! Eli: Shouldn't this inherieting DisposableObject?
67
{
78
private IntPtr _handle;
89

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,12 @@ public void prevent_fetching(Operation op)
439439
_unfetchable_ops.Add(op);
440440
}
441441

442-
protected override void DisposeManagedState()
442+
protected override void DisposeManagedResources()
443443
{
444444
ops.default_graph_stack.remove(this);
445445
}
446446

447-
protected override void DisposeUnManagedState(IntPtr handle)
447+
protected override void DisposeUnmanagedResources(IntPtr handle)
448448
{
449449
c_api.TF_DeleteGraph(handle);
450450
}

src/TensorFlowNET.Core/Graphs/ImportGraphDefOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void AddReturnOutput(string name, int index)
3737
c_api.TF_ImportGraphDefOptionsAddReturnOutput(_handle, name, index);
3838
}
3939

40-
protected override void DisposeUnManagedState(IntPtr handle)
40+
protected override void DisposeUnmanagedResources(IntPtr handle)
4141
=> c_api.TF_DeleteImportGraphDefOptions(handle);
4242

4343
public static implicit operator IntPtr(ImportGraphDefOptions opts) => opts._handle;

src/TensorFlowNET.Core/Sessions/BaseSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public void close()
396396
Dispose();
397397
}
398398

399-
protected override void DisposeUnManagedState(IntPtr handle)
399+
protected override void DisposeUnmanagedResources(IntPtr handle)
400400
{
401401
using (var status = new Status())
402402
{

src/TensorFlowNET.Core/Sessions/SessionOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public SessionOptions(IntPtr handle)
3232
_handle = handle;
3333
}
3434

35-
protected override void DisposeUnManagedState(IntPtr handle)
35+
protected override void DisposeUnmanagedResources(IntPtr handle)
3636
=> c_api.TF_DeleteSessionOptions(handle);
3737

3838
public void SetConfig(ConfigProto config)

src/TensorFlowNET.Core/Status/Status.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void SetStatus(TF_Code code, string msg)
5050
/// </summary>
5151
public void Check(bool throwException = false)
5252
{
53-
if(Code != TF_Code.TF_OK)
53+
if (Code != TF_Code.TF_OK)
5454
{
5555
Console.WriteLine(Message);
5656
if (throwException)
@@ -65,7 +65,7 @@ public static implicit operator IntPtr(Status status)
6565
return status._handle;
6666
}
6767

68-
protected override void DisposeUnManagedState(IntPtr handle)
68+
protected override void DisposeUnmanagedResources(IntPtr handle)
6969
=> c_api.TF_DeleteStatus(handle);
7070
}
71-
}
71+
}

0 commit comments

Comments
 (0)