Browse Source

Add IsAlive property to IRef<T> (#20593)

pull/20108/merge
Julien Lebosquain 3 days ago
committed by GitHub
parent
commit
0fee13f821
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      src/Avalonia.Base/Media/Imaging/Bitmap.cs
  2. 6
      src/Avalonia.Base/Utilities/Ref.cs

4
src/Avalonia.Base/Media/Imaging/Bitmap.cs

@ -291,9 +291,7 @@ namespace Avalonia.Media.Imaging
{ {
get get
{ {
// TODO12: We should probably make PlatformImpl to be nullable or make it possible to check if (!PlatformImpl.IsAlive)
// and fix IRef<T> in general (right now Item is not nullable while it internally is)
if (PlatformImpl.Item == null!)
return null; return null;
return PlatformImpl; return PlatformImpl;
} }

6
src/Avalonia.Base/Utilities/Ref.cs

@ -28,6 +28,10 @@ namespace Avalonia.Utilities
/// <returns>A reference to the value as the new type but sharing the refcount.</returns> /// <returns>A reference to the value as the new type but sharing the refcount.</returns>
IRef<TResult> CloneAs<TResult>() where TResult : class; IRef<TResult> CloneAs<TResult>() where TResult : class;
/// <summary>
/// Gets whether the reference still tracks a valid item.
/// </summary>
bool IsAlive { get; }
/// <summary> /// <summary>
/// The current refcount of the object tracked in this reference. For debugging/unit test use only. /// The current refcount of the object tracked in this reference. For debugging/unit test use only.
@ -172,6 +176,8 @@ namespace Avalonia.Utilities
return new Ref<TResult>(item, counter); return new Ref<TResult>(item, counter);
} }
public bool IsAlive => _item is not null;
public int RefCount => _counter?.RefCount ?? throw new ObjectDisposedException("Ref<" + typeof(T) + ">"); public int RefCount => _counter?.RefCount ?? throw new ObjectDisposedException("Ref<" + typeof(T) + ">");
} }
} }

Loading…
Cancel
Save