Do not throw from Finializer

This commit is contained in:
Christian Köllner 2019-09-04 17:59:59 +02:00
parent 05661ae9bb
commit f237086515
2 changed files with 20 additions and 1 deletions

View File

@ -136,7 +136,24 @@ namespace Capnp.Rpc
{ {
if (!_disposedValue) if (!_disposedValue)
{ {
ConsumedCap?.Release(); if (disposing)
{
ConsumedCap?.Release();
}
else
{
// When called from the Finalizer, we must not throw.
// But when reference counting goes wrong, ConsumedCapability.Release() will throw an InvalidOperationException.
// The only option here is to suppress that exception.
try
{
ConsumedCap?.Release();
}
catch
{
}
}
_disposedValue = true; _disposedValue = true;
} }
} }

View File

@ -70,6 +70,8 @@ namespace Capnp.Rpc
{ {
if (++_refCount <= 1) if (++_refCount <= 1)
{ {
--_refCount;
throw new ObjectDisposedException(nameof(ConsumedCapability)); throw new ObjectDisposedException(nameof(ConsumedCapability));
} }
} }