From 0fee13f821e406b808f77b8157a1f4f5c8ee2176 Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Wed, 4 Feb 2026 03:14:51 +0000 Subject: [PATCH] Add IsAlive property to IRef (#20593) --- src/Avalonia.Base/Media/Imaging/Bitmap.cs | 4 +--- src/Avalonia.Base/Utilities/Ref.cs | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Base/Media/Imaging/Bitmap.cs b/src/Avalonia.Base/Media/Imaging/Bitmap.cs index 9c0c20d170..63ec02737f 100644 --- a/src/Avalonia.Base/Media/Imaging/Bitmap.cs +++ b/src/Avalonia.Base/Media/Imaging/Bitmap.cs @@ -291,9 +291,7 @@ namespace Avalonia.Media.Imaging { get { - // TODO12: We should probably make PlatformImpl to be nullable or make it possible to check - // and fix IRef in general (right now Item is not nullable while it internally is) - if (PlatformImpl.Item == null!) + if (!PlatformImpl.IsAlive) return null; return PlatformImpl; } diff --git a/src/Avalonia.Base/Utilities/Ref.cs b/src/Avalonia.Base/Utilities/Ref.cs index 0b6abe5189..49a7a42710 100644 --- a/src/Avalonia.Base/Utilities/Ref.cs +++ b/src/Avalonia.Base/Utilities/Ref.cs @@ -28,6 +28,10 @@ namespace Avalonia.Utilities /// A reference to the value as the new type but sharing the refcount. IRef CloneAs() where TResult : class; + /// + /// Gets whether the reference still tracks a valid item. + /// + bool IsAlive { get; } /// /// 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(item, counter); } + public bool IsAlive => _item is not null; + public int RefCount => _counter?.RefCount ?? throw new ObjectDisposedException("Ref<" + typeof(T) + ">"); } }