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

Binding a property in datatemplate via a named control caused memory leak #15444

Closed
lfeng1420 opened this issue Apr 19, 2024 · 10 comments · Fixed by #15485
Closed

Binding a property in datatemplate via a named control caused memory leak #15444

lfeng1420 opened this issue Apr 19, 2024 · 10 comments · Fixed by #15485
Labels

Comments

@lfeng1420
Copy link

Describe the bug

When binding a property from viewmodel in datatemplate through a named control, memory leak occured. see below code:

<ListBox x:Name="elems" ItemsSource="{Binding Elements}">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="vm:Element">
                <Grid ColumnDefinitions="Auto,*,Auto">
                    <CheckBox IsChecked="{Binding IsSelected}" />
                    <TextBlock
                        Grid.Column="1"
                        VerticalAlignment="Center"
                        Text="{Binding Text}" />
                    <TextBlock
                        Grid.Column="2"
                        VerticalAlignment="Center"
                        Text="{Binding #elems.((vm:MainViewModel)DataContext).Greeting}" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

if replace '#elems' with '$parent[ListBox]', everything will be fine.

To Reproduce

  1. clone this repo
  2. build project and run
  3. scroll from home to end, then from end to home (see below video)

repeat step 3 multiple times, and you will see Process Memory stays high and never fall back

memory-leak.mov

Expected behavior

no memory leak occurs.

Avalonia version

11.0.10

OS

Windows

Additional context

No response

@lfeng1420 lfeng1420 added the bug label Apr 19, 2024
@timunie
Copy link
Contributor

timunie commented Apr 19, 2024

test also 11.1 beta pls

@workgroupengineering
Copy link
Contributor

Same as #15443

@lfeng1420
Copy link
Author

test also 11.1 beta pls

i tried 11.1.0-beta1, also exists.

@lfeng1420
Copy link
Author

Same as #15443

oh, i'll close this issue

@BAndysc
Copy link
Contributor

BAndysc commented Apr 19, 2024

I am not sure it is the same leak, I think it is a different thing. I think it is better to keep it open, memory leaks are a big problem, better keep track of all repros.

@BAndysc
Copy link
Contributor

BAndysc commented Apr 19, 2024

This leak is caused by NamedElementNode keeping a strong reference via _value

private object? _value = AvaloniaProperty.UnsetValue;

so it is a different problem than #15443

Looks like it was introduced with binding refactor

@lfeng1420 lfeng1420 reopened this Apr 20, 2024
@lfeng1420
Copy link
Author

I am not sure it is the same leak, I think it is a different thing. I think it is better to keep it open, memory leaks are a big problem, better keep track of all repros.

reopen now~

@maxkatz6
Copy link
Member

@BAndysc note, you attached file from 11.1 version, while this issue is reproduced on 11.0.

11.0.10 code doesn't seem to use strong references there: https://github.com/AvaloniaUI/Avalonia/blob/11.0.10/src/Avalonia.Base/Data/Core/ExpressionNode.cs

@BAndysc
Copy link
Contributor

BAndysc commented Apr 21, 2024

That's true, I didn't take a look at 11.0 branch, and also, my conclusion that the leak is caused by NamedElementNode having a strong reference is not accurate, to say the least. I mean, it is the whole path from root which matters. A screenshot if it is any help:

image

The right path shows a leak where named binding is involved, with my zero knowledge about styling and binding system, I can't tell where the leak "begins".

As a non paying user I am not in position to complain, but imho those leaks are quite a big problem, affecting vast majority of the user base (even if they are unaware).

@timunie
Copy link
Contributor

timunie commented Apr 22, 2024

We're nit saying these leaks couldn't get a problem. So better to fix them st some point in time. If you want to help us speeding it up, you can also add your sample into src/Samples/Sandbox and try to debug it. From there you can step through the whole binding path. Again, I don't force anyone to do that, just an idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants