Welcome, Guest

Author Topic: Disposing WPF BrowserViewControl  (Read 336 times)

mdavis

  • Posts: 2
    • View Profile
Disposing WPF BrowserViewControl
« on: September 01, 2017, 08:10:56 AM »
Main Question: What is the correct way to dispose of a WPF BrowserViewControl, so that all resources and process created by it are released and closed.

Details:
In our system we have the need to be able to create and destroy multiple BrowserViewControl's.

We noticed that if we don't explicitly dispose of the controls it will keep making CoherentUI processes to the point where it will starve the system of resources. Our current attempt to dispose of the controls seemed to be working and getting rid of the processes. However, while the processes no longer exist in the process list, resources are still being taken up to the point our software crashes with the following stack trace:

Code: [Select]
	
System.ArgumentNullException: Value cannot be null.
Parameter name: source
   at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object destination, Int32 startIndex, Int32 length)
   at Coherent.UI.TextureBufferHelperImpl.MapSharedMemory(CoherentHandle handle, Int32 width, Int32 height) in f:\repo\ui-build\Coherent\UI\DotNET\TextureBufferHelperImpl.cs:line 82
   at Coherent.UI.Windows.Controls.DefaultViewListener.DrawSharedMemory(CoherentHandle handle, Int32 width, Int32 height) in f:\repo\ui-build\Coherent\UI\DotNET\Controls\WPF\ListenersWpf.cs:line 196
   at Coherent.UI.Windows.Controls.DefaultViewListener.OnDraw(CoherentHandle handle, Boolean usesSharedMemory, Int32 width, Int32 height) in f:\repo\ui-build\Coherent\UI\DotNET\Controls\WPF\ListenersWpf.cs:line 185
   at Coherent.UI.BrowserViewListener.SwigDirectorOnDraw(IntPtr handle, Boolean usesSharedMemory, Int32 width, Int32 height) in f:\repo\ui-build\Coherent\UI\DotNET\swig\Universal\BrowserViewListener.cs:line 483
   at Coherent.UI.CoherentUI_NativePINVOKE_x86.ViewContext_FetchSurfaces(HandleRef jarg1)
   at Coherent.UI.ViewContext.FetchSurfaces() in f:\repo\ui-build\Coherent\UI\DotNET\swig\Universal\ViewContext.cs:line 54
   at Coherent.UI.Windows.Controls.BrowserViewContext.m_Ticker_Tick(Object sender, EventArgs e) in f:\repo\ui-build\Coherent\UI\DotNET\Controls\WPF\BrowserViewContextWpf.cs:line 88
   at System.Windows.Threading.DispatcherTimer.FireTick(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

Our current approach is to call BrowserViewControl.BrowserView.GetContext().Dispose(), then BrowserViewControl.Dispose().

My attempts to call BrowserView.Destroy(), ViewContext.Uninitalize(), BrowserViewContext.Dispose, and ViewContext.Dispose() in various orders/combinations hasn't seemed to work and usually produces an immediate exception.

Thanks,
Matt

john.v

  • Administrator
  • *****
  • Posts: 68
    • View Profile
Re: Disposing WPF BrowserViewControl
« Reply #1 on: September 01, 2017, 10:10:49 AM »
Hi mtdavis,

In order to destroy a BrowserViewControl and create a new one in its place, you just have to do the following:

               
Code: [Select]
 //Add the new view Control to the children and the row definitions of the main grid

                BrowserViewControl newControl = new BrowserViewControl();
                this.gridMain.Children.Remove(viewControl);
               
                //Destroy the BrowserView and remove the BrowserViewControl's handlers
                viewControl.BrowserView.Destroy();
                viewControl.Dispose();

                //Assign the new BrowserViewControl
                viewControl = newControl;
               
                //Add the new one to the grid
                this.gridMain.Children.Add(viewControl);
                Grid.SetRow(newControl, this.gridMain.RowDefinitions.Count - 1);

Please let us know if this helps. If the problem still happens on your end, please contact us at support@coherent-labs.com and send us a minimal sample that demonstrates the issues you are having so we could take a look at it and investigate them further.

John V.
Support Engineer at Coherent Labs

mdavis

  • Posts: 2
    • View Profile
Re: Disposing WPF BrowserViewControl
« Reply #2 on: September 05, 2017, 05:50:24 AM »
Thank you for the quick reply. I went ahead and followed that exact order of execution and things appeared to be working. It turns out something was holding on to a reference of the browser view control and that was preventing the exe instances from being closed and released. However, I've run in to another issue. I let our system run over the weekend and when I came in I found an exception the following callstack:

Code: [Select]
   at System.Windows.Media.Imaging.WriteableBitmap..ctor(Int32 pixelWidth, Int32 pixelHeight, Double dpiX, Double dpiY, PixelFormat pixelFormat, BitmapPalette palette)
   at Coherent.UI.Windows.Controls.BrowserViewControl.CreateTexture() in f:\repo\ui-build\Coherent\UI\DotNET\Controls\WPF\BrowserViewControlWpf.xaml.cs:line 158
   at Coherent.UI.Windows.Controls.DefaultContextListener.ContextReady() in f:\repo\ui-build\Coherent\UI\DotNET\Controls\WPF\ListenersWpf.cs:line 72
   at Coherent.UI.ContextListener.SwigDirectorContextReady() in f:\repo\ui-build\Coherent\UI\DotNET\swig\Universal\ContextListener.cs:line 112
   at Coherent.UI.CoherentUI_NativePINVOKE_x86.ViewContext_Update(HandleRef jarg1)
   at Coherent.UI.ViewContext.Update() in f:\repo\ui-build\Coherent\UI\DotNET\swig\Universal\ViewContext.cs:line 50
   at Coherent.UI.Windows.Controls.BrowserViewContext.m_Ticker_Tick(Object sender, EventArgs e) in f:\repo\ui-build\Coherent\UI\DotNET\Controls\WPF\BrowserViewContextWpf.cs:line 83
   at System.Windows.Threading.DispatcherTimer.FireTick(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

Along with a windows OS error message stating:dllhost.exe - application error: The exception unknown software exception (0x0000000e) occurred in the application at location 0x00007FFDFEF49E08. There were additional messages that were very similar to that one that appeared after I hit OK.

After accepting the errors and continuing after the exception, the system seems to have returned to working order. Although the memory consumption still seems a little high, but I can't verify the source.

Any thoughts?

Thanks again,
Matt


john.v

  • Administrator
  • *****
  • Posts: 68
    • View Profile
Re: Disposing WPF BrowserViewControl
« Reply #3 on: October 05, 2017, 03:09:38 AM »
Hi Matt,

Could you please create a small sample which demonstrates the crash and send it to support@coherent-labs.com? Alternatively, you can send us instructions which we can follow to reproduce it in our sample. That way we will be able to investigate this further.

John V.
Support Engineer at Coherent Labs
« Last Edit: October 06, 2017, 02:11:42 AM by john.v »

Tags: