Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rafalka opened this issue May 30, 2024 · 3 comments · Fixed by #22807
Assignees
Labels
area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events fixed-in-8.0.60 fixed-in-9.0.0-preview.5.24307.10 platform/android 🤖 platform/iOS 🍎 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@rafalka
Copy link
Contributor

rafalka commented May 30, 2024

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

@rafalka rafalka added the t/bug Something isn't working label May 30, 2024
Copy link
Contributor

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@rafalka rafalka changed the title PlatformBehavior.OnAttachedTo() is called twice during adding behaviour to a control PlatformBehavior.OnAttachedTo() is called twice during adding behaviour to a control causing a memory leak May 31, 2024
@kevinxufei kevinxufei added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed platform/android 🤖 platform/iOS 🍎 labels May 31, 2024
@kevinxufei
Copy link

Verified this issue on Visual Studio for Mac 17.6.12 build 410 (8.0.40/8.0.21) with sample project. Can repro it.

@PureWeen
Copy link
Member

Possibly related
#21916

@PureWeen PureWeen added the area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events label May 31, 2024
@PureWeen PureWeen added this to the .NET 9 Planning milestone May 31, 2024
@PureWeen PureWeen modified the milestones: .NET 9 Planning, .NET 8 SR6 Jun 4, 2024
@PureWeen PureWeen self-assigned this Jun 4, 2024
@PureWeen PureWeen moved this from Todo to In Progress in MAUI SDK Ongoing Jun 4, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in MAUI SDK Ongoing Jun 4, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events fixed-in-8.0.60 fixed-in-9.0.0-preview.5.24307.10 platform/android 🤖 platform/iOS 🍎 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants