microsoft / automatic-graph-layout

A set of tools for graph layout and viewing
Other
1.35k stars 302 forks source link

svgGraphViewer Throws Exception On Close() After Write() #273

Closed Salami-Sam closed 3 years ago

Salami-Sam commented 3 years ago

I have this code in a wpf window's Loaded event handler. Just above this block of code is where I create "graph". The line "svgGraphWriter.Close();" at the end throws an exception. svgGraphWriter.Write(); creates the file and writes it to "path" without issues, so this is weird to me. I would think this would be a straightforward use of svgGraphWriter, since you don't want to keep its Stream open once the svg is created. Below are the exception details.

GraphRenderer renderer = new GraphRenderer(graph); renderer.CalculateLayout(); SvgGraphWriter svgGraphWriter = new SvgGraphWriter(new FileStream(path, FileMode.OpenOrCreate), graph); svgGraphWriter.AllowedToWriteUri = true; svgGraphWriter.Write(); svgGraphWriter.Close();

System.InvalidOperationException HResult=0x80131509 Message=The Writer is closed or in error state. Source=System.Xml StackTrace: at System.Xml.XmlWellFormedWriter.AdvanceState(Token token) at System.Xml.XmlWellFormedWriter.WriteEndElement() at Microsoft.Msagl.Drawing.SvgGraphWriter.Close() at PantserNoteTaker.Windows.AnalysisWindow.BuildGraph(Object sender, RoutedEventArgs e) in C:\Users\rfish\source\repos\Salami-Sam\PantserNoteTaker\PantserNoteTaker\Windows\AnalysisWindow.xaml.cs:line 100 at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent) at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root) at MS.Internal.LoadedOrUnloadedOperation.DoWork() at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks() at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget) at System.Windows.Interop.HwndTarget.OnResize() at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam) at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 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) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

This exception was originally thrown at this call stack: [External Code] PantserNoteTaker.Windows.AnalysisWindow.BuildGraph(object, System.Windows.RoutedEventArgs) in AnalysisWindow.xaml.cs [External Code]

Salami-Sam commented 3 years ago

Ah. It looks like it works when you don't call Close() after Write().