Skip to content

PlatformBehavior.OnAttachedTo() is called twice during adding behaviour to a control causing a memory leak #22740

@rafalka

Description

@rafalka

Description

PlatformBehavior.OnAttachedTo() is called twice during adding behaviour to a control.

As captured using a sample project, this method is called first time from:

BehaviourInitTwice.TestBehaviour.OnAttachedTo(VisualElement bindable, View platformView) in BehaviourInitTwice/TestBehaviour.cs:line 22
    at Microsoft.Maui.Controls.PlatformBehavior`2[[Microsoft.Maui.Controls.VisualElement, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].OnLoaded(Object sender, EventArgs e) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\PlatformBehavior.cs:line 107
    at Microsoft.Maui.Controls.VisualElement.SendLoaded(Boolean updateWiring) in D:\a\_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2187
    at Microsoft.Maui.Controls.VisualElement.HandlePlatformUnloadedLoaded() in D:\a\_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.Platform.cs:line 30
    at Microsoft.Maui.Controls.VisualElement.UpdatePlatformUnloadedLoadedWiring(Window newWindow, Window oldWindow) in D:\a\_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2275
    at Microsoft.Maui.Controls.VisualElement.add_Loaded(EventHandler value) in D:\a\_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2137
    at Microsoft.Maui.Controls.PlatformBehavior`2[[Microsoft.Maui.Controls.VisualElement, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].OnAttachedTo(VisualElement bindable) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\PlatformBehavior.cs:line 47
    at Microsoft.Maui.Controls.Behavior`1[[Microsoft.Maui.Controls.VisualElement, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnAttachedTo(BindableObject bindable) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\Behavior.cs:line 68
    at Microsoft.Maui.Controls.PlatformBehavior`2[[Microsoft.Maui.Controls.VisualElement, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].OnAttachedTo(BindableObject bindable) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\PlatformBehavior.cs:line 33
    at Microsoft.Maui.Controls.Behavior.Microsoft.Maui.Controls.IAttachedObject.AttachTo(BindableObject bindable) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\Behavior.cs:line 32
    at Microsoft.Maui.Controls.AttachedCollection`1[[Microsoft.Maui.Controls.Behavior, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].InsertItem(Int32 index, Behavior item) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\AttachedCollection.cs:line 53
    at System.Collections.ObjectModel.Collection`1[[Microsoft.Maui.Controls.Behavior, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Add(Behavior item)

and second time from:

BehaviourInitTwice.TestBehaviour.OnAttachedTo(VisualElement bindable, View platformView) in BehaviourInitTwice/TestBehaviour.cs:line 22
    at Microsoft.Maui.Controls.PlatformBehavior`2[[Microsoft.Maui.Controls.VisualElement, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].OnLoaded(Object sender, EventArgs e) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\PlatformBehavior.cs:line 107
    at Microsoft.Maui.Controls.VisualElement.add_Loaded(EventHandler value) in D:\a\_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2139
    at Microsoft.Maui.Controls.PlatformBehavior`2[[Microsoft.Maui.Controls.VisualElement, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].OnAttachedTo(VisualElement bindable) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\PlatformBehavior.cs:line 47
    at Microsoft.Maui.Controls.Behavior`1[[Microsoft.Maui.Controls.VisualElement, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnAttachedTo(BindableObject bindable) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\Behavior.cs:line 68
    at Microsoft.Maui.Controls.PlatformBehavior`2[[Microsoft.Maui.Controls.VisualElement, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].OnAttachedTo(BindableObject bindable) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\PlatformBehavior.cs:line 33
    at Microsoft.Maui.Controls.Behavior.Microsoft.Maui.Controls.IAttachedObject.AttachTo(BindableObject bindable) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\Behavior.cs:line 32
    at Microsoft.Maui.Controls.AttachedCollection`1[[Microsoft.Maui.Controls.Behavior, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].InsertItem(Int32 index, Behavior item) in D:\a\_work\1\s\src\Controls\src\Core\Interactivity\AttachedCollection.cs:line 53
    at System.Collections.ObjectModel.Collection`1[[Microsoft.Maui.Controls.Behavior, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Add(Behavior item)

This issue causes a memory leak when using a MCT TouchBehaviour

Steps to Reproduce

  1. Create a .NET MAUI app
  2. Create a dummy Platform behaviour
  3. Place a breakpoint in OnAttachedTo()
  4. Create a page with layout and button
  5. In button click handler create dummy behaviour and add it to a layout
  6. Debug app and observer that OnAttachedTo() is called twice

Link to public reproduction project repository

https://github.com/rafalka/BehaviourInitTwice/

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, macOS, I was not able test on other platforms

Affected platform versions

iOS 17, Android 13, MAC 14.4.1

Did you find any workaround?

Workarounding this issue is not possible without changing platform behaviour implementation

Relevant log output

No response

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions