Message.Attach memory leak

Dec 24, 2010 at 11:48 AM
Edited Dec 24, 2010 at 11:49 AM

see attached code,

steps:
1.Launch the program.
2.Click Add button
3.Click the added item in the Listbox
4.Click the RemoveAll button
5.Click the report button which use  ObjectTracker to detect memory leak.

result: testdata is leaked.

ps: If I skip step3, or remove the Message.Attach, then no memory leak.

<UserControl x:Class="Caliburn_MemoryLeak.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:cal="http://www.caliburnproject.org"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="400">
    <UserControl.Resources>
        <DataTemplate x:Key="TestDataTemplate">
            <Rectangle Fill="Green"
                       Width="200"
                       Height="200"
                       cal:Message.Attach="[Event MouseLeftButtonDown]=[Action Test]" />
        </DataTemplate>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot"
          Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="265*" />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal"
                    HorizontalAlignment="Center">
            <Button Content="Add"
                    Margin="8,0"
                    HorizontalAlignment="Left"
                    Name="btnAdd"
                    VerticalAlignment="Top"
                    Click="btnAdd_Click" />
            <Button Content="Remove All"
                    Margin="8,0"
                    HorizontalAlignment="Left"
                    Name="btnRemoveAll"
                    VerticalAlignment="Top"
                    Click="btnRemoveAll_Click" />

            <Button Content="Report"
                    Margin="8,0"
                    HorizontalAlignment="Left"
                    Name="btnReport"
                    VerticalAlignment="Top"
                    Click="btnReport_Click" />
        </StackPanel>

        <ListBox Grid.Row="1"
                 ItemsSource="{Binding Items}"
                 ItemTemplate="{StaticResource TestDataTemplate}">
        </ListBox>
    </Grid>
</UserControl>

    public partial class MainPage : UserControl
    {
        public class TestData 
        {
            public void Test() { }
        }

        ObservableCollection<TestData> _items = new ObservableCollection<TestData>();
        public ObservableCollection<TestData> Items { get { return _items; } }

        public MainPage()
        {
            InitializeComponent();
            DataContext = this;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var data = new TestData();
            ObjectTracker.Track(data);
            _items.Add(data);
        }

        private void btnRemoveAll_Click(object sender, RoutedEventArgs e)
        {
            _items.Clear();
        }

        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            ObjectTracker.Report();
        }
    }
Coordinator
Dec 24, 2010 at 12:58 PM

Can you please open an issue and attach a repro project to the ticket. It often times takes me more time to create the repro project than it does to fix the bug. So that would greatly expedite this if you want it fixed. Better yet would be to recommend a fix to the leak...