HakanL / WkHtmlToPdf-DotNet

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
GNU Lesser General Public License v3.0
367 stars 66 forks source link

Memory leak in SynchronizedConverter #17

Closed Airex closed 3 years ago

Airex commented 4 years ago

in case of using of SynchronizedConverter PdfTools is as singleton and contains _Delegates member that is never cleared and list just fills with delegates on each conversion

_Delegates.Clear(); in DestroyConverter should remove this issue

xamele0n commented 3 years ago

I purpose to change "delegates" to

 //used to maintain a reference to delegates to prevent them being garbage collected...
private readonly ConcurrentDictionary<IntPtr, List<object>> delegates = new ConcurrentDictionary<IntPtr, List<object>>();

and use

        public void DestroyConverter(IntPtr converter)
        {
            this.delegates.TryRemove(converter, out var l);
            l?.Clear();
            this.module.wkhtmltopdf_destroy_converter(converter);
        }

to free associated delegates

HakanL commented 3 years ago

Please submit PR for review

HakanL commented 3 years ago

I've implemented this now in version 1.3.0, please verify.