Page 1 of 1

Potential memory leak in 2014.2.1924 (WPF)

Posted: Wed Oct 08, 2014 6:12 am
by sandynith
Hi,

I am trying the latest prerelease build 2014.2.1924 for rendering the charts (e.g. StiLineSeries) which has improved performance for the charts.
However I noticed that if you run multiple big reports few time then you run out of memory quickly and this problem was NOT there with 2014.1 full release build.

After debugging, I noticed that after disposing off the report the memory is not released by Stimulsoft components and looks like the KeyEventHandler is still referenced.
I used red gate ANTS memory profiler to detect this and attaching the retention graph showing the same.

Using reflector I drilled down the assemblies and looks like in StiWpfViewerControl class in the ControlLoaded method you guys register for KeyEventHandler and but no when is un-registering it, which is causing this problem and the chain in retention graph is not breaking and hence GC is not able to claim memory.

Code: Select all

             //The StiWpfViewerControl class
               void IComponentConnector.Connect(int connectionId, object target)
		{
			switch (connectionId)
			{
			case 1:
				this.viewerControl = (StiWpfViewerControl)target;
				this.viewerControl.SizeChanged += new SizeChangedEventHandler(this.UserControl_SizeChanged);
				this.viewerControl.PreviewDrop += new System.Windows.DragEventHandler(this.Viewer_PreviewDrop);
				this.viewerControl.Loaded += new RoutedEventHandler(this.ControlLoaded);
				return;
                     .........
                     .........

               //Registering for the KeyEventHandler but couldn't find anyone un-registering it.
		private void ControlLoaded(object sender, RoutedEventArgs e)
		{
			Window parentWindow = this.GetParentWindow(this);
			parentWindow.KeyDown += new System.Windows.Input.KeyEventHandler(this.WindowOnKeyDown);
		}

Could you please look into this problem and advice?
Providing a sample project showing problem will be time consuming for me.

Thanks,
Sandeep

Re: Potential memory leak

Posted: Thu Oct 09, 2014 3:45 am
by ReubenB
Hey,

I've noticed this memory leak too, I am certain it is caused by the WindowOnKeyDown event handler.
I tried removing the event handler on the StiWpfViewerControl unloaded event using reflection and it fixes the memory leak.
Like so,

Code: Select all

        private void unloadEventHandler(object o, RoutedEventArgs a)
        {
            MethodInfo mi = typeof(StiWpfViewerControl).GetMethod("WindowOnKeyDown", BindingFlags.NonPublic | BindingFlags.Instance);
            Delegate d = mi.CreateDelegate(typeof(KeyEventHandler), m_reportPreview);
            m_parentWindow.RemoveHandler(Keyboard.KeyDownEvent, d);

            m_reportPreview.Unloaded -= unloadEventHandler;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            m_reportPreview = this.GetPartFromTemplate<StiWpfViewerControl>("ReportPreview");
            m_reportPreview.Unloaded += unloadEventHandler;
            ...
        }
But that's a terribly dirty hack. The StiWpfViewerControl should be removing the event handler itself at some point.

Cheers,
Reuben.

Re: Potential memory leak

Posted: Thu Oct 09, 2014 12:25 pm
by HighAley
Hello.

We have made the Dispose() method of StiWpfViewerControl public. You could use it to solve memory leak problem.
This method will be available in our next major release.

Thank you.

Re: Potential memory leak

Posted: Thu Oct 09, 2014 10:31 pm
by sandynith
Hi Aleksey,
Ideally you should unhook the event handler at some point, so it could be the Dispose method as well.
So, do you mean that you will unhook the event in the Dispose method and we should call the Dispose method?

Thanks,
Sandeep

Re: Potential memory leak in 2014.2.1924 (WPF)

Posted: Mon Oct 13, 2014 3:45 am
by sandynith
Hi Aleksey,
Today I updated to WPF 2014.2 full release and can verify that calling of Dispose method of StiWpfViewerControl does unhook the event and now that part of memory is getting freed. Its quite a relief. Thanks for that :-)

However I noticed that even after disposing off memory still there are Sti DataRows and DataTable hanging around. We do the following to clear the Sti components:
+ call Dispose of StiWpfViewerControl
+ report.DataStore.Clear(); //report is StiReport
report.Dictionary.DataStore.Clear();
report.Dispose();

Even after doing this the data rows are still present and being referenced by the StiChartInteractionHelper.cachedChart and cachedChartGeom as shown in the attached retention graph.
Because of this huge amount of memory couldn't be freed even after disposing off the report and hence we quickly run out of memory if report is run multiple times.

I am not sure if there is any way to not use the cached chart, or what are the benefits of using it etc. (Interaction or DrillDown is not enabled on my report Charts)
I noticed that there is StiChartInteractionHelper.ResetChartGeomCache() public function - but not sure if its safe to call it.

Could you please advice here?

Thanks,
Sandeep

Re: Potential memory leak in 2014.2.1924 (WPF)

Posted: Wed Oct 15, 2014 6:55 am
by HighAley
Hello.

Thank you for your huge help to improve our product.
We have made some changes following your advices.
Please, check our next prerelease build.

Thank you.

Re: Potential memory leak in 2014.2.1924 (WPF)

Posted: Thu Oct 16, 2014 9:01 pm
by sandynith
Hi Aleksey,

Thanks for your efforts and addressing the issues. I will check the next pre-release build to see how it goes.
Cheers,
Sandeep

Re: Potential memory leak in 2014.2.1924 (WPF)

Posted: Fri Oct 17, 2014 11:11 am
by HighAley
Hello.

Let us know if you need any additional help.

Thank you.

Re: Potential memory leak in 2014.2.1924 (WPF)

Posted: Mon Oct 27, 2014 10:19 pm
by sandynith
Hi Aleksey,

I checked our reports using latest pre-release build WPF 2014.2.2001 and can confirm that now we don't see the memory leaks as reported in this post.
Now disposing off a report properly release memory and it looks pretty good. Thanks for addressing this issue.

Cheers,
Sandeep

Re: Potential memory leak in 2014.2.1924 (WPF)

Posted: Tue Oct 28, 2014 8:29 am
by Andrew
Hello,

Thank you for letting us know about this!