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

DataContext bindings in ControlTemplate leak memory #15443

Open
BAndysc opened this issue Apr 19, 2024 · 4 comments
Open

DataContext bindings in ControlTemplate leak memory #15443

BAndysc opened this issue Apr 19, 2024 · 4 comments

Comments

@BAndysc
Copy link
Contributor

BAndysc commented Apr 19, 2024

Describe the bug

Having a datacontext binding in a ControlTemplate, like:

<TabStrip.Styles>
  <Style Selector="TabStripItem">
    <Setter Property="Template">
      <ControlTemplate>
        <DockPanel>
          <TextBlock x:DataType="avaloniaBugs:BigFileViewModel" Text="{CompiledBinding Title}" />
        </DockPanel>
      </ControlTemplate>
    </Setter>
  </Style>

leads to memory leaks. This can be workaround with an ItemTemplate, I think this use case should either work as expected or it should be be completely disallowed with a xaml compiler error.

To Reproduce

  1. git clone https://github.com/BAndysc/avalonia-bugs-repro
  2. git checkout bug_template_binding_leak
  3. Run the app with a memory profiler, i.e. dotMemory
  4. Press "New Tab"
  5. Close the tabs
  6. Observe the memory is not freed

image

Without bindings in Template:
Open MainWindow.axaml, comment lines https://github.com/BAndysc/avalonia-bugs-repro/blob/bug_template_binding_leak/AvaloniaBugs/MainWindow.axaml#L25-L26

Do the same steps - memory will be freed now.

Expected behavior

No response

Avalonia version

0.10, 11.0.x and latest nightly

OS

No response

Additional context

No response

@BAndysc BAndysc added the bug label Apr 19, 2024
@BAndysc BAndysc changed the title Binding in ControlTemplate leaks memory DataContext bindings in ControlTemplate leaks memory Apr 19, 2024
@BAndysc BAndysc changed the title DataContext bindings in ControlTemplate leaks memory DataContext bindings in ControlTemplate leak memory Apr 19, 2024
@MrJul
Copy link
Member

MrJul commented Apr 23, 2024

There are two leaks here:

@BAndysc
Copy link
Contributor Author

BAndysc commented Apr 25, 2024

With those two PRs it works much better now. There is, however, still one small "semi-leak"

  1. Press Add Tab three times
  2. Press Close All Tabs

One of the tabs will not be freed, until a new tab is opened

image

SelectedValue is not cleared, even tho SelectedItem was set to null

image

Of course this is not as serious as the previous leak, since upon opening a new tab, SelectedValue will be updated again and the old tab will be freed.

@MrJul
Copy link
Member

MrJul commented Apr 25, 2024

The remaining problem here is that SelectionChanged isn't raised when the items collection is cleared (despite SelectedItem being changed). Because of that issue, the handler setting SelectedValue isn't being called.

@workgroupengineering
Copy link
Contributor

@MrJul another unhandled scenario from this example is this #15455. Do I create a new issue?

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

No branches or pull requests

4 participants