maui-toolkit: NullReferenceException in ShimmerDrawable.OnAnimationUpdate
Description
My MAUI iOS application logs crashes to Sentry where I see the following crash for a small percentage of my users.
System.NullReferenceException: Object reference not set to an instance of an object
?, in void ShimmerDrawable.OnAnimationUpdate(double animationValue)
?, in Animation(Action<double> callback, double start, double end, Easing easing, Action finished)+(double f) => { }
?, in Action<double> Animation.GetCallback()+(double f) => { }
?, in void AnimationExtensions.AnimateInternal<T>(IAnimatable self, IAnimationManager animationManager, string name, Func<double, T> transform, Action<T> callback, uint rate, uint length, Easing easing, Action<T, bool> finished, Func<bool> repeat)+(double f) => { }
?, in void AnimationExtensions.HandleTweenerUpdated(object o, EventArgs args)
?, in bool Tweener.Step(long step)
?, in void TweenerAnimation.OnTick(double millisecondsSinceLastUpdate)
?, in void Animation.Tick(double milliseconds)
?, in void AnimationManager.OnFire()+OnAnimationTick(Animation animation)
?, in void List<Animation>.ForEach(Action<Animation>)
?, in void AnimationManager.OnFire()
?, in void PlatformTicker.Start()+() => { }
?, in void NSActionDispatcher.Apply()
?, in void __Registrar_Callbacks__.callback_1476_Foundation_NSActionDispatcher_Apply(IntPtr pobj, IntPtr sel, IntPtr* exception_gchandle)
Looks like the only reference that can be null in this method is the WeakReference<IShimmer> Shimmer - and that perhaps it was garbage collected after the null check. Should this be converted to a pattern match cast into a local variable instead?
if (Shimmer is not null shimmer)
{
return;
}
float offset = (float)animationValue;
// Calculating the wave width in factor value.
float waveWidthFactor = (float)(shimmer.WaveWidth / _availableSize.Width); //use local variable
Disclaimers: I am still using SyncFusion.Maui.Core but after decompiling ShimmerDrawable, the code appears to be the same between SyncFusion Maui Toolkit and SyncFusion Essential Studio. I do plan on switching over soon and will do so to test any fix.
I am using shimmers with CustomViews
I am using custom code to aggressively clean up my views
protected override void OnParentSet()
{
if (Parent == null)
{
//wrap dispose in try-catch until we catch all binding reevalutation NREs
try
{
Dispose();
}
catch (Exception e)
{
#if DEBUG
System.Diagnostics.Debugger.Break();
#endif
Handler?.MauiContext?.Services.GetService<ILoggingService>()?.Error(e);
}
}
base.OnParentSet();
}
/// <summary>
/// Clear all resources from the view tree (like Maui should be doing)
/// </summary>
/// <param name="element"></param>
private void ClearAllResources(IVisualTreeElement element)
{
if (ClearChildren(element))
{
// Clear resources from the bottom up
foreach (var child in element.GetVisualChildren())
if (child is VisualElement childElement)
ClearAllResources(childElement);
}
// Clear the items that get stuck in memory
if (element is View view)
{
//ListViewRenderer doesn't implement DisconnectHander until Maui 9.0 https://github.com/dotnet/maui/pull/25453
//Can remove after .net 9 upgrade if CollectionChanged is properly unsubscribed and doesn't cause NRE on clearing page BindingContext
if (view.Handler is IDisposable disposableHandler)
{
disposableHandler.Dispose();
}
view.Handler?.DisconnectHandler();
view.Behaviors?.Clear();
view.Effects?.Clear();
view.Triggers?.Clear();
view.ClearGestureListeners();
view.ClearKeyboardListeners();
view.ClearTouchListeners();
}
// Dispose of the element
if (element is IDisposable disposable)
disposable.Dispose();
}
private static bool ClearChildren(IVisualTreeElement element)
{
//Skip SfDataGrid which will throw NREs when data context is later set to null if children are cleared
return element is not Syncfusion.Maui.DataGrid.SfDataGrid;
}
/// <summary>
/// Implementation of the unmanaged dispose pattern
/// </summary>
/// <param name="disposing">false when called from a finalizer, and true when called from the IDisposable.Dispose method. </param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
_disposed = true;
_messenger.UnregisterAll(this);
if (BindingContext is BaseViewModel baseViewModel)
{
baseViewModel?.Dispose();
}
Handler?.DisconnectHandler();
ClearAllResources(Content);
BindingContext = null;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~BaseContentPage()
{
Dispose(false);
}
Is there some way to stop the animations during cleanup? I see theres animation cleanup when IsActive is set to false https://github.com/syncfusion/maui-toolkit/blob/main/maui/src/Shimmer/SfShimmer.cs#L643 (and I see that sometimes IsActive can be true when ClearAllResources is called). I can try setting IsActive to false myself but is there somewhere the control should also be terminating the animation itself?
Steps to Reproduce
No response
Version with bug
1.0.4
Is this a regression from previous behavior?
Not sure, haven’t tested other versions
Last Known Working Version
1.0.3
Affected platforms
iOS, I was not able test on other platforms
Affected Platform Versions
No response
Have you found a workaround?
No response
Relevant log output
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 21 (8 by maintainers)
Hi @LeoJHarris
We’re glad to inform you that we have addressed this problem by implementing proper null checking in the OnAnimationUpdate method. We have also prepared a custom patch (NuGet package) that includes this fix. I’ve attached this patch below for your convenience. Please feel free to download and apply the update to see if it resolves the issue on your end. If you continue to experience the NullReferenceException, we would appreciate any additional information you can provide, especially details about the scenarios where the issue occurs. This would be extremely helpful for us to investigate further and ensure a comprehensive resolution
Root Cause: Null Exception thrown on OnAnimation Method
Patch :
Syncfusion.Maui.Toolkit.1.0.4.zip
Note: Before installing the patch, please follow the steps below to remove the old cache NuGet and check your application with the provided NuGet:
Disclaimer: Please note that the patch was prepared specifically for version 1.0.4. We will include the issue fix in our upcoming release. We will let you know once the NuGet release is rolled out and appreciate your patience until then.
Regards, Muniappan S