Bug 53417 - Clearing Effects collection results in NullReferenceException
Summary: Clearing Effects collection results in NullReferenceException
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: 2.3.3
Hardware: Macintosh Mac OS
: Normal major
Target Milestone: ---
Assignee: Chris King
URL:
Depends on:
Blocks:
 
Reported: 2017-03-16 21:15 UTC by Tim Klingeleers
Modified: 2017-07-17 07:53 UTC (History)
5 users (show)

Tags: effect clear crash ac
Is this bug a regression?: ---
Last known good build:


Attachments
Solution that shows the issue, originally from Lesson 2 of Xamarin University XAM330 (449.19 KB, application/zip)
2017-03-16 21:24 UTC, Tim Klingeleers
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Tim Klingeleers 2017-03-16 21:15:01 UTC
# Steps to reproduce
- Start with the solution from XAM330 of Xamarin University (https://university.xamarin.com/materials/using-effects-in-xamarinforms-xam330) under: Lab Materials/Part 02 Resources/Completed/ControlExplorer.sln
- Change line 28 in MainPage.xaml.cs of the PCL project (ControlExplorer) from 
labelWelcome.Effects.Remove(fontEffect);
to
labelWelcome.Effects.Clear();
- Run the application and turn the switch to the off-position

# Expected behavior
- Clear all effects on a control without NullReferenceExceptions

# Actual behavior
- A NullReferenceException is thrown because the EffectsOnClearing method of the Element class loops over all effects and calls the ClearEffect on it. But the Effects collection also contains a null Effect initially.

# Supplemental info (logs, images, videos)
Stacktrace of NullReferenceException:
  at Xamarin.Forms.Element.EffectsOnClearing (System.Object sender, System.EventArgs eventArgs) [0x00014] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Element.cs:523 
  at Xamarin.Forms.TrackableCollection`1[T].ClearItems () [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\TrackableCollection.cs:12 
  at System.Collections.ObjectModel.Collection`1[T].Clear () [0x00017] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/collections/objectmodel/collection.cs:75 
  at ControlExplorer.MainPage.OnSwitchToggled (System.Object sender, Xamarin.Forms.ToggledEventArgs e) [0x0000c] in /Users/tim/Downloads/using-effects-in-xamarinforms-xam330/Lab Materials/Part 02 Resources/Completed/ControlExplorer/ControlExplorer/MainPage.xaml.cs:28 
  at (wrapper delegate-invoke) System.EventHandler`1[Xamarin.Forms.ToggledEventArgs]:invoke_void_object_TEventArgs (object,Xamarin.Forms.ToggledEventArgs)
  at Xamarin.Forms.Switch+<>c.<.cctor>b__10_0 (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x0000f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Switch.cs:13 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x00108] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:584 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0014b] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:378 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:324 
  at Xamarin.Forms.Element.Xamarin.Forms.IElementController.SetValueFromRenderer (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Element.cs:249 
  at Xamarin.Forms.Platform.Android.SwitchRenderer.Android.Widget.CompoundButton.IOnCheckedChangeListener.OnCheckedChanged (Android.Widget.CompoundButton buttonView, System.Boolean isChecked) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\SwitchRenderer.cs:16 
  at Android.Widget.CompoundButton+IOnCheckedChangeListenerInvoker.n_OnCheckedChanged_Landroid_widget_CompoundButton_Z (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_buttonView, System.Boolean isChecked) [0x00011] in /Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Widget.CompoundButton.cs:86 
  at (wrapper dynamic-method) System.Object:2d66780f-670c-4e89-acdb-9b74746becb1 (intptr,intptr,intptr,bool)

# Test environment (full version information)
Xamarin Forms: 2.3.3.180
Xamarin Studio: 6.2
Android: any version
iOS: any version
OS: macOS Sierra
Comment 1 Tim Klingeleers 2017-03-16 21:24:29 UTC
Created attachment 20393 [details]
Solution that shows the issue, originally from Lesson 2 of Xamarin University XAM330
Comment 2 Paul DiPietro [MSFT] 2017-03-17 02:38:08 UTC
Checked the project against 2.3.4-pre5 for good measure and the crash still appears to be occurring.
Comment 4 Rui Marinho 2017-03-17 23:05:38 UTC
Should be fixed on 2.3.5-pre1
Comment 5 Saurabh Paunikar 2017-07-17 07:53:33 UTC
Verified on xamarin.form version 2.3.5.256-pre6

ScreenCast link : https://www.screencast.com/t/4PrIJhiN6in