File tree 1 file changed +33
-1
lines changed
1 file changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,36 @@ internal static void CheckSize ()
108
108
}
109
109
}
110
110
111
+ internal class DefaultGraphStack
112
+ {
113
+ [ ThreadStatic ]
114
+ private static IList < TFGraph > stack ;
115
+ private static object lockObj = new object ( ) ;
116
+
117
+ internal TFGraph Instance => stack . Last ( ) ;
118
+
119
+ internal static void SetGraph ( TFGraph graph )
120
+ {
121
+ if ( stack == null )
122
+ {
123
+ lock ( lockObj )
124
+ {
125
+ if ( stack == null )
126
+ {
127
+ stack = new List < TFGraph > ( ) ;
128
+ }
129
+ }
130
+ }
131
+
132
+ stack . Add ( graph ) ;
133
+ }
134
+
135
+ internal static void RemoveGraph ( TFGraph graph )
136
+ {
137
+ stack . Remove ( graph ) ;
138
+ }
139
+ }
140
+
111
141
/// <summary>
112
142
/// Base class for many TensorFlow data types that provides a common idiom to dispose and
113
143
/// release resources associated with the native data types. Generally, you do not need to use this.
@@ -473,19 +503,21 @@ public partial class TFGraph : TFDisposable
473
503
/// <summary>
474
504
/// Initializes a new instance of the <see cref="T:TensorFlow.TFGraph"/> class.
475
505
/// </summary>
476
- public TFGraph ( ) : base ( TF_NewGraph ( ) )
506
+ public TFGraph ( ) : this ( TF_NewGraph ( ) )
477
507
{
478
508
}
479
509
480
510
internal TFGraph ( IntPtr handle ) : base ( handle )
481
511
{
512
+ DefaultGraphStack . SetGraph ( this ) ;
482
513
}
483
514
484
515
// extern void TF_DeleteGraph (TF_Graph *);
485
516
[ DllImport ( NativeBinding . TensorFlowLibrary ) ]
486
517
static extern unsafe void TF_DeleteGraph ( TF_Graph graph ) ;
487
518
internal override void NativeDispose ( IntPtr handle )
488
519
{
520
+ DefaultGraphStack . RemoveGraph ( this ) ;
489
521
TF_DeleteGraph ( handle ) ;
490
522
}
491
523
You can’t perform that action at this time.
0 commit comments