File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
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 ;
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.
@@ -479,13 +509,15 @@ public TFGraph () : base (TF_NewGraph ())
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