Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Memory Leak in PointAnnotation

I use some PointAnnotation in my graph. Each time the data is updated - I remove all the PointAnnotation and add news.
After I do it many times - I got an OutOfMemory exception.
I found that there is a memory leak in PointAnnotation and as a result - all the points I removed actually stayed in memory.

Here is a simple code to get it:

xaml:

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ni="http://schemas.ni.com/controls/2009/xaml/presentation"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ni:Graph x:Name="graph"/>
        <Button Grid.Row="1" Click="Button_Click">Add Point Annotations</Button>
    </Grid>
</Window>

xaml.cs:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            PointAnnotation point = new PointAnnotation();
            point.Fill = new SolidColorBrush(Colors.Red);
            point.HorizontalPosition = 5;
            point.VerticalPosition = 5;
            point.TargetShape = PointShape.Ellipse;
            point.TargetSize = new Size(10, 10);
            point.ArrowVisibility = System.Windows.Visibility.Collapsed;
            point.InteractionMode = AnnotationInteractionModes.None;
            graph.Children.Add(point);
            Thread.Sleep(800);

            Task t = new Task(() =>
            {
                for (int i = 0; i < int.MaxValue; i++)
                {
                    Application.Current.Dispatcher.Invoke(() =>
                        {
                            graph.Children.RemoveAt(0);
                            point = new PointAnnotation();
                            point.Fill = new SolidColorBrush(Colors.Red);
                            point.HorizontalPosition = i % 11;
                            point.VerticalPosition = i % 11;
                            point.TargetShape = PointShape.Ellipse;
                            point.TargetSize = new Size(10, 10);
                            point.ArrowVisibility = System.Windows.Visibility.Collapsed;
                            point.InteractionMode = AnnotationInteractionModes.None;
                            graph.Children.Add(point);
                        });
                    Thread.Sleep(70);
                }
            });
            t.Start();
        }
    }

This code only adds a point and removes it in a loop from 0 to int.MaxValue.
I get this exception after I add about 80000 points. (to do it faster - you can remove the Thread.Sleep)

 

Is there a workaround I can add to my code to fix this issue (beyond reusing of the same instances)?

0 Kudos
Message 1 of 3
(2,536 Views)

I was able to reproduce the issue locally, and have created a task to fix it. Unfortunately, I could not find any simple workaround — I believe a workaround may be possible (by wrapping the data mapper for the axes, and manually clearing the point annotation event registrations when it is removed from the graph), but it seems like this will require significantly more code than simply re-using annotation instances.

~ Paul H
0 Kudos
Message 2 of 3
(2,490 Views)

Just wanted to let you know this issue was fixed in the Measurement Studio 2019 release.

~ Paul H
0 Kudos
Message 3 of 3
(1,791 Views)