diff options
author | Bret Johnson <bret.johnson@microsoft.com> | 2022-08-01 22:11:55 +0300 |
---|---|---|
committer | Bret Johnson <bret.johnson@microsoft.com> | 2022-08-01 22:11:55 +0300 |
commit | 7b7e98dfafc2f5435ad27f3343b9e6492cb619fb (patch) | |
tree | d8e13275f2b1c8b2a59a61647864c75e28213df5 | |
parent | 5964a0ee68900ef62dfa65a4150643876b2945ba (diff) |
Use GC.KeepAlive to prevent CGColor getting releaseddev/netonjm/fix-1569130
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/Custom/HistoryLayer.cs | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/HistoryLayer.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/HistoryLayer.cs index 48baebb..d4ec241 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/HistoryLayer.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/HistoryLayer.cs @@ -3,6 +3,7 @@ using AppKit; using CoreAnimation; using CoreGraphics; using ObjCRuntime; +using Xamarin.PropertyEditing.Drawing; namespace Xamarin.PropertyEditing.Mac { @@ -77,13 +78,20 @@ namespace Xamarin.PropertyEditing.Mac public override void UpdateFromModel (EditorInteraction interaction) { LayoutIfNeeded (); - current.BackgroundColor = interaction.Color.ToCGColor (); + SetCALayerBackgroundColor (current, interaction.Color); //there are some scenarios where viewmodel could be null - var initialColor = interaction.ViewModel?.InitialColor; - if (initialColor != null) { - previous.BackgroundColor = initialColor.ToCGColor (); - } - last.BackgroundColor = interaction.LastColor.ToCGColor (); + if (interaction.ViewModel != null) + SetCALayerBackgroundColor (previous, interaction.ViewModel.InitialColor); + SetCALayerBackgroundColor (last, interaction.LastColor); + } + + static void SetCALayerBackgroundColor(CALayer layer, CommonColor color) + { + // We need to use GC.KeepAlive to ensure that the CGColor doesn't get garbage collected (and thus the + // native object Released) before the BackgroundColor assignment is complete + CGColor cgColor = color.ToCGColor (); + layer.BackgroundColor = cgColor; + GC.KeepAlive (cgColor); } public override void UpdateFromLocation (EditorInteraction interaction, CGPoint location) |