SVG icon indicating copy to clipboard operation
SVG copied to clipboard

Out of memory Exception

Open radamand opened this issue 7 years ago • 11 comments

Application failes for 523KB file with exception {"Out of memory."} Data:{System.Collections.ListDictionaryInternal} Looks like due to file with many tags Dictionary couldn't hold massive data at System.Drawing.Graphics.CheckErrorStatus(Int32 status) at System.Drawing.Graphics.DrawPath(Pen pen, GraphicsPath path) at Svg.SvgRenderer.DrawPath(Pen pen, GraphicsPath path) in D:\SVG\SVG-master\Source\Rendering\SvgRenderer.cs:line 55 at Svg.SvgVisualElement.RenderStroke(ISvgRenderer renderer) in D:\SVG\SVG-master\Source\Basic Shapes\SvgVisualElement.cs:line 283 at Svg.SvgPath.RenderStroke(ISvgRenderer renderer) in D:\SVG\SVG-master\Source\Paths\SvgPath.cs:line 132 at Svg.SvgVisualElement.Render(ISvgRenderer renderer, Boolean renderFilter) in D:\SVG\SVG-master\Source\Basic Shapes\SvgVisualElement.cs:line 173 at Svg.SvgVisualElement.Render(ISvgRenderer renderer) in D:\SVG\SVG-master\Source\Basic Shapes\SvgVisualElement.cs:line 129 at Svg.SvgElement.RenderChildren(ISvgRenderer renderer) in D:\SVG\SVG-master\Source\SvgElement.cs:line 726 at Svg.SvgVisualElement.Render(ISvgRenderer renderer, Boolean renderFilter) in D:\SVG\SVG-master\Source\Basic Shapes\SvgVisualElement.cs:line 183 at Svg.SvgVisualElement.Render(ISvgRenderer renderer) in D:\SVG\SVG-master\Source\Basic Shapes\SvgVisualElement.cs:line 129 at Svg.SvgElement.RenderChildren(ISvgRenderer renderer) in D:\SVG\SVG-master\Source\SvgElement.cs:line 726 at Svg.SvgVisualElement.Render(ISvgRenderer renderer, Boolean renderFilter) in D:\SVG\SVG-master\Source\Basic Shapes\SvgVisualElement.cs:line 183 at Svg.SvgVisualElement.Render(ISvgRenderer renderer) in D:\SVG\SVG-master\Source\Basic Shapes\SvgVisualElement.cs:line 129 at Svg.SvgElement.RenderChildren(ISvgRenderer renderer) in D:\SVG\SVG-master\Source\SvgElement.cs:line 726 at Svg.SvgElement.Render(ISvgRenderer renderer) in D:\SVG\SVG-master\Source\SvgElement.cs:line 714 at Svg.SvgFragment.Render(ISvgRenderer renderer) in D:\SVG\SVG-master\Source\Document Structure\SvgFragment.cs:line 169 at Svg.SvgDocument.Draw(Bitmap bitmap) in D:\SVG\SVG-master\Source\SvgDocument.cs:line 502 at Svg.SvgDocument.Draw() in D:\SVG\SVG-master\Source\SvgDocument.cs:line 466 at SVGViewer.SVGViewer.RenderSvg(SvgDocument svgDoc) in D:\SVG\SVG-master\Samples\SVGViewer\SvgViewer.cs:line 45 at SVGViewer.SVGViewer.open_Click(Object sender, EventArgs e) in D:\SVG\SVG-master\Samples\SVGViewer\SvgViewer.cs:line 28 at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at SVGViewer.Program.Main() in D:\SVG\SVG-master\Samples\SVGViewer\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

radamand avatar Feb 21 '17 16:02 radamand

Note that sometimes an out of memory exception is thrown by System.Graphics if trying to draw very small objects (I ran into this recently). See for example https://social.msdn.microsoft.com/Forums/vstudio/en-US/4c0aa2b2-6555-4c6f-85fc-8467e86bdf20/systemdrawinggraphicsdrawpath-throws-outofmemoryexception-when-drawing-a-very-small-cubic-bezier?forum=netfxbcl.

mrbean-bremen avatar Feb 21 '17 17:02 mrbean-bremen

path stroke-width="0.2" stroke-linecap="square" stroke-dasharray="1 0.5" d="M79.623 47.197C79.609 47.204 79.596 47.212 79.582 47.219"/ This record fails the whole solution. Any suggestions hot to fix in the Svg solution?

radamand avatar Feb 22 '17 10:02 radamand

Looks good for me, but maybe the created structures are indeed too small. You may try to increase the difference between the coordinates in the curve part of the path just to check if this is the problem.

mrbean-bremen avatar Feb 22 '17 13:02 mrbean-bremen

Increased curve to 89 and it works ok. Only one question is how to fix that in code because I cant affect source image because it's a blueprint.

radamand avatar Feb 22 '17 15:02 radamand

The only possibility I see currently is to add checks in the SVG library for very small sizes (this can happen for paths and other objects) and skip the rendering in this case as a workaround for the bug in .NET drawing. Also, maybe the problem is gone with a newer .NET version? (though I doubt it)

mrbean-bremen avatar Feb 22 '17 16:02 mrbean-bremen

i am afraid this is a GDI bug... hard to tell.

tebjan avatar Feb 22 '17 16:02 tebjan

If I exclude very small size my blueprint looks pure. Only long lines are visible

radamand avatar Feb 23 '17 16:02 radamand

Fix in SvgVisualElements.cs resolves a problem try { renderer.DrawPath(pen, path); } catch { }

radamand avatar Feb 24 '17 08:02 radamand

I think I have read that catching this kind of exception that come from GDI does not work in release mode - but this may be wrong. Be sure that it works in release mode, though. Also, it would make sense to catch only the out of memory exception, and log some warning - the exception also happens with some elements that have zero size, and these maybe programming errors.

mrbean-bremen avatar Feb 24 '17 10:02 mrbean-bremen

There is another issue in drawing text tag when export to png. Text placement is wrong(a bit above). But whe I debug everything works ok

radamand avatar Feb 24 '17 11:02 radamand

We're bumping into it as well, too small paths seem to be causing it. Any chance the workaround of catching the OutOfMemoryException may make it? It would then at least render all other elements.

I'm using this workaround now, but it may of course hide too much:

private void HideSmallElements(SvgElement element)
{
	if (element is ISvgBoundable boundable)
	{
		var size = boundable.Bounds;
		if (size.Width < 3 && size.Height < 3)
		{
			element.Visibility = "hidden";
		}
	}
	foreach (var c in element.Children)
	{
		HideSmallElements(c);
	}
}

debruyckere avatar Oct 15 '20 16:10 debruyckere