Skip to content

Memory leak in LayoutInflater.Inflate method when using custom view in XML layout. #11101

@anton-yashin

Description

@anton-yashin

Android framework version

net10.0-android

Affected platform version

VS 18.4.11626.88 & 36.1.30/10.0.100

Description

The LayoutInflater.Inflate method can cause a memory leak if the XML layout contains a reference to a custom .net view. A regression has been observed in net10-android. The same code works correctly in net9-android.

Minimal repro in attachment. The example contains a reference counter. You can switch the project from net10-android to net9-android to see the change in behavior.

net10-android-memory-leak-on-inflate.zip

Steps to Reproduce

  1. Create new net10-android application
  2. Add custom view
internal sealed class CustomView : global::Android.Views.View
        {
            public CustomView(Context? context) : base(context) { } 
            public CustomView(nint javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }
            public CustomView(Context? context, IAttributeSet? attrs) : base(context, attrs) { }
            public CustomView(Context? context, IAttributeSet? attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) { }
            public CustomView(Context? context, IAttributeSet? attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) { }
        }
  1. Add layout
<FrameLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
	>

	<AndroidApp2.CustomView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
		/>
	
</FrameLayout>
  1. Inflate it's somewhere
LayoutInflater.Inflate(Resource.Layout.test, null, false);
  1. Observe memory leak

android-memory-leak.zip

Did you find any workaround?

Nothing.

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: App RuntimeIssues in `libmonodroid.so`.needs-triageIssues that need to be assigned.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions