Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering a gradient using SvgDocument.Draw() runs out of memory in Svg 3.4.2 #989

Closed
aewjones opened this issue Jul 12, 2022 · 0 comments · Fixed by #991
Closed

Rendering a gradient using SvgDocument.Draw() runs out of memory in Svg 3.4.2 #989

aewjones opened this issue Jul 12, 2022 · 0 comments · Fixed by #991

Comments

@aewjones
Copy link

Description

Rendering an SvgDocument containing a gradient to a bitmap runs out of memory if the document is repeatedly rendered.

Example data

Example project that repeatedly renders an SvgDocument to a Bitmap and displays the bitmap in a PictureBox.
SvgGradientTest.zip

Used Versions

Svg 3.4.2 (problem does not occur in earlier versions)
Windows 11
.NET 6

Additional information

This problem may be related to issue #978, which also uses a gradient.

The problem appears to be caused by SvgGradientServer.LoadStops(), which was introduced in version 3.4.2:

        private void LoadStops(SvgVisualElement parent)
        {
            var gradient = this;
            while (gradient?.Stops.Count == 0)
                gradient = SvgDeferredPaintServer.TryGet<SvgGradientServer>(gradient.InheritGradient, parent);

            if (gradient != null)
                Stops.AddRange(gradient.Stops);
        }

If this.Stops.Count is nonzero on entry, Stops.AddRange(gradient.Stops) will append this.Stops to itself. If this is done repeatedly, the length of this.Stops increases geometrically, leading to an out-of-memory condition. One solution is to check for this condition:

        private void LoadStops(SvgVisualElement parent)
        {
            var gradient = this;
            while (gradient?.Stops.Count == 0)
                gradient = SvgDeferredPaintServer.TryGet<SvgGradientServer>(gradient.InheritGradient, parent);

            if (gradient != null && gradient != this)
                Stops.AddRange(gradient.Stops);
        }
mrbean-bremen pushed a commit that referenced this issue Jul 13, 2022
* fix LoadStops in SvgGradientServer
* add test
* fixes #989
github-actions bot pushed a commit that referenced this issue Jul 13, 2022
…UTING.md Generators Nuget README.md Samples Source Tests doc docfx.json index.md license.txt fix LoadStops in SvgGradientServer BuildProcessTemplates CONTRIBUTING.md Generators Nuget README.md Samples Source Tests doc docfx.json index.md license.txt add test BuildProcessTemplates CONTRIBUTING.md Generators Nuget README.md Samples Source Tests doc docfx.json index.md license.txt fixes #989
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant