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
366 stars 66 forks source link

PdfTools.GetConversionResult doesn't frees memory #129

Closed SavoFortech closed 1 month ago

SavoFortech commented 3 months ago

hi, we have noticed that if we call the convesion for two html in the same time, sometimes happens that the second one will be generated with errors, specifically, the second one, is generated correctly but seems that a "piece" of the first one is been appended to it.

Watching the implementation we noticed that the method GetConversionResult, in the PdfTools class, doesn't call the "Marshal.FreeHGlobal", like other method does after has been called the "Marshal.Copy" function.

Can you pleade check if it'll be make sense add "Marshal.FreeHGlobal" GetConversionResult?

HakanL commented 3 months ago

I'm pretty sure we shouldn't call Marshal.FreeHGlobal on GetConversionResult since the buffer is allocated in the native library, vs the other places where Marshal.Copy is used where we allocate memory with Marshal.AllocHGlobal. It sounds more like your issue is that you're not using the SynchronizedConverter.

SavoFortech commented 3 months ago

hi, thank you for reply, im instantiate it in startup.cs for my DI with: services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

But we face with the problem very very rarely, normally it works well. For this reason we are thinking in a sort of race condition

HakanL commented 3 months ago

Ok, yeah that is correct. I don't have any other ideas on what may cause your issue, since this is just a wrapper around the native library, it is possible there is an issue in the native library, or an issue with the interop.

SavoFortech commented 1 month ago

Sorry, for not replying to you but the issue never prompt anymore and i forget about this ticket. If we face with the problem again in the future we will try to investigate more. Thanks for your time.