@@ -14,39 +14,47 @@ namespace TorchSharp
14
14
/// </summary>
15
15
public sealed class DisposeScope : IDisposable
16
16
{
17
- private readonly DisposeScopeManager _disposeScopeManager ;
17
+ private DisposeScopeManager ? _disposeScopeManager ;
18
18
19
- public DisposeScope ( DisposeScopeManager disposeScopeManager )
19
+ internal DisposeScope ( DisposeScopeManager disposeScopeManager )
20
20
{
21
- _disposeScopeManager = disposeScopeManager ?? throw new ArgumentNullException ( nameof ( disposeScopeManager ) ) ;
22
- if ( disposeScopeManager . DisposeScopeStack . Count > 0 ) {
23
- OuterScope = disposeScopeManager . DisposeScopeStack [
24
- disposeScopeManager . DisposeScopeStack . Count - 1 ] ;
25
- }
21
+ _disposeScopeManager = disposeScopeManager ;
22
+ this . OuterScope = disposeScopeManager . CurrentDisposeScope ;
26
23
}
27
24
28
25
/// <summary>
29
26
/// The outer scope with relation to this scope.
30
27
/// </summary>
31
- internal DisposeScope ? OuterScope { get ; }
28
+ internal DisposeScope ? OuterScope { get ; set ; }
32
29
33
30
/// <summary>
34
31
/// The disposables that are scheduled for disposing.
35
32
/// </summary>
36
- /// TODO: There is a ReferenceEqualityComparer coming in .NET 6, use that!
37
33
internal HashSet < IDisposable > Disposables { get ; private set ; } =
38
34
new HashSet < IDisposable > ( ReferenceEqualityComparer < IDisposable > . Default ) ;
39
35
40
36
/// <summary>
41
37
/// A view of the disposables in the scope - this list will not be kept in synch with the disposables
42
38
/// in the scope.
43
39
/// </summary>
44
- public IReadOnlyList < IDisposable > DisposablesView => Disposables . ToList ( ) ;
40
+ public IReadOnlyList < IDisposable > DisposablesView {
41
+ get {
42
+ if ( this . _disposeScopeManager is null )
43
+ throw new ObjectDisposedException ( "The dispose scope has been disposed." ) ;
44
+ return Disposables . ToArray ( ) ;
45
+ }
46
+ }
45
47
46
48
/// <summary>
47
49
/// The number of disposables currently held in the scope
48
50
/// </summary>
49
- public int DisposablesCount => Disposables . Count ;
51
+ public int DisposablesCount {
52
+ get {
53
+ if ( this . _disposeScopeManager is null )
54
+ throw new ObjectDisposedException ( "The dispose scope has been disposed." ) ;
55
+ return Disposables . Count ;
56
+ }
57
+ }
50
58
51
59
/// <summary>
52
60
/// Includes a disposable in the scope - for tensors this is done automatically once the scope has been
@@ -57,6 +65,8 @@ public DisposeScope(DisposeScopeManager disposeScopeManager)
57
65
/// <returns></returns>
58
66
public T Include < T > ( T disposable ) where T : IDisposable
59
67
{
68
+ if ( this . _disposeScopeManager is null )
69
+ throw new ObjectDisposedException ( "The dispose scope has been disposed." ) ;
60
70
Disposables . Add ( disposable ) ;
61
71
return disposable ;
62
72
}
@@ -157,6 +167,8 @@ public void MoveToOther(DisposeScope? scope, params IDisposable[] disposables) =
157
167
/// </summary>
158
168
public void MoveToOther ( DisposeScope ? scope , IEnumerable < IDisposable > disposables )
159
169
{
170
+ if ( this . _disposeScopeManager is null )
171
+ throw new ObjectDisposedException ( "The dispose scope has been disposed." ) ;
160
172
foreach ( var disposable in disposables ) {
161
173
if ( Disposables . Remove ( disposable ) ) {
162
174
AddToOther ( scope , disposable ) ;
@@ -208,6 +220,8 @@ public T Detach<T>(T disposable) where T : IDisposable
208
220
/// </summary>
209
221
public void Detach ( IEnumerable < IDisposable > disposables )
210
222
{
223
+ if ( this . _disposeScopeManager is null )
224
+ throw new ObjectDisposedException ( "The dispose scope has been disposed." ) ;
211
225
foreach ( var disposable in disposables ) {
212
226
if ( Disposables . Remove ( disposable ) ) {
213
227
_disposeScopeManager . StatisticsInstance . DetachedFromScopeCount ++ ;
@@ -220,6 +234,8 @@ public void Detach(IEnumerable<IDisposable> disposables)
220
234
221
235
public void Attach ( IDisposable disposable )
222
236
{
237
+ if ( this . _disposeScopeManager is null )
238
+ throw new ObjectDisposedException ( "The dispose scope has been disposed." ) ;
223
239
if ( disposable is torch . Tensor tensor ) {
224
240
if ( tensor . OwningDisposeScope == null && ! tensor . IsInvalid ) {
225
241
_disposeScopeManager . StatisticsInstance . DetachedFromScopeCount -- ;
@@ -241,6 +257,8 @@ public void Attach(IDisposable disposable)
241
257
/// </summary>
242
258
public void DisposeEverythingBut ( IEnumerable < IDisposable > inKeep )
243
259
{
260
+ if ( this . _disposeScopeManager is null )
261
+ throw new ObjectDisposedException ( "The dispose scope has been disposed." ) ;
244
262
// Avoiding multiple enumerations
245
263
var oldList = Disposables ;
246
264
Disposables = inKeep . ToHashSet ( ReferenceEqualityComparer < IDisposable > . Default ) ;
@@ -316,8 +334,11 @@ public T DisposeEverythingBut<T>(T keep) where T : IDisposable
316
334
/// </summary>
317
335
public void Dispose ( )
318
336
{
337
+ if ( this . _disposeScopeManager is null )
338
+ return ;
319
339
DisposeEverything ( ) ;
320
340
_disposeScopeManager . RemoveDisposeScope ( this ) ;
341
+ this . _disposeScopeManager = null ;
321
342
}
322
343
323
344
/// <summary>
@@ -329,6 +350,8 @@ public void Dispose()
329
350
/// <param name="disposable">The disposable that was disposed</param>
330
351
public void MarkAsDisposed ( IDisposable disposable )
331
352
{
353
+ if ( this . _disposeScopeManager is null )
354
+ throw new ObjectDisposedException ( "The dispose scope has been disposed." ) ;
332
355
_disposeScopeManager . StatisticsInstance . DisposedInScopeCount ++ ;
333
356
Disposables . Remove ( disposable ) ;
334
357
if ( disposable is torch . Tensor tensor ) {
@@ -345,6 +368,8 @@ public void MarkAsDisposed(IDisposable disposable)
345
368
346
369
private void AddToOther ( DisposeScope ? scope , IDisposable disposable )
347
370
{
371
+ if ( this . _disposeScopeManager is null )
372
+ throw new ObjectDisposedException ( "The dispose scope has been disposed." ) ;
348
373
if ( scope != null ) {
349
374
scope . Disposables . Add ( disposable ) ;
350
375
} else {
0 commit comments