Skip to content

Commit

Permalink
Prevent duplicate disposal of owned instances
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Nov 19, 2024
1 parent 15a5433 commit 534aca6
Showing 1 changed file with 68 additions and 39 deletions.
107 changes: 68 additions & 39 deletions src/Pure.DI.Core/Components/Api.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,26 +1243,37 @@ internal interface IOwned
#endif
internal sealed partial class Owned : global::System.Collections.Generic.List<object>, global::Pure.DI.IOwned
{
private volatile bool _isDisposed;

/// <inheritdoc />
public void Dispose()
{
for (var i = Count - 1; i >= 0; i--)
if (_isDisposed)
{
return;
}

_isDisposed = true;
try
{
switch (this[i])
for (var i = Count - 1; i >= 0; i--)
{
case global::Pure.DI.IOwned _:
break;
switch (this[i])
{
case global::Pure.DI.IOwned _:
break;

case global::System.IDisposable disposableInstance:
try
{
disposableInstance.Dispose();
}
catch (global::System.Exception exception)
{
OnDisposeException(disposableInstance, exception);
}
break;
case global::System.IDisposable disposableInstance:
try
{
disposableInstance.Dispose();
}
catch (global::System.Exception exception)
{
OnDisposeException(disposableInstance, exception);
}

break;

#if NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
case global::System.IAsyncDisposable asyncDisposableInstance:
Expand All @@ -1280,46 +1291,64 @@ public void Dispose()
}
break;
#endif
}
}
}
finally
{
Clear();
}
}

#if NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
/// <inheritdoc />
public async global::System.Threading.Tasks.ValueTask DisposeAsync()
{
for (var i = Count - 1; i >= 0; i--)
if (_isDisposed)
{
switch (this[i])
return;
}

_isDisposed = true;
try
{
for (var i = Count - 1; i >= 0; i--)
{
case global::Pure.DI.IOwned _:
break;
switch (this[i])
{
case global::Pure.DI.IOwned _:
break;

case global::System.IAsyncDisposable asyncDisposableInstance:
try
{
await asyncDisposableInstance.DisposeAsync();
}
catch (global::System.Exception exception)
{
OnDisposeAsyncException(asyncDisposableInstance, exception);
}
break;
case global::System.IAsyncDisposable asyncDisposableInstance:
try
{
await asyncDisposableInstance.DisposeAsync();
}
catch (global::System.Exception exception)
{
OnDisposeAsyncException(asyncDisposableInstance, exception);
}
break;

case global::System.IDisposable disposableInstance:
try
{
disposableInstance.Dispose();
}
catch (global::System.Exception exception)
{
OnDisposeException(disposableInstance, exception);
}
break;
case global::System.IDisposable disposableInstance:
try
{
disposableInstance.Dispose();
}
catch (global::System.Exception exception)
{
OnDisposeException(disposableInstance, exception);
}
break;
}
}
}
finally
{
Clear();
}
}
#endif
#endif


/// <summary>
Expand Down

0 comments on commit 534aca6

Please sign in to comment.