PDFiumSharp icon indicating copy to clipboard operation
PDFiumSharp copied to clipboard

A callback was made on a garbage collected delegate

Open EddyRts opened this issue 6 months ago • 1 comments

We are experiencing a runtime error when converting multiple pages of a PDF document to a PDFiumBitmap, caused by the following error:

A callback was made on a garbage collected delegate of type 'PDFiumSharp!PDFiumSharp.Types.FileReadBlockHandler::Invoke'.

The code:

using (var memoryStream = new MemoryStream())
{
	await aAspNetCoreFormFile.CopyToAsync(memoryStream).ConfigureAwait(false);
	memoryStream.Position = 0L;

	var fileRead = FPDF_FILEREAD.FromStream(memoryStream);

	using (var doc = new PdfDocument(memoryStream, fileRead))
	{
		foreach (var page in doc.Pages)
		{
			using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, false))
			{
				// set white background
				bitmap.FillRectangle(0, 0, (int)page.Width, (int)page.Height, new FPDF_COLOR(255, 255, 255));

				using (var stream = new MemoryStream())
				{
					page.Render(bitmap);
					bitmap.Save(stream);
					using (Image img = Image.FromStream(stream))
					{
						using (var outputStream = new MemoryStream())
						{
							img.Save(outputStream, jpgEncoder, myEncoderParameters);
							//Save the outputStream.ToArray() to the DB
						}
					}
				}
			}
		}
		doc.Close();
	}
}

The stacktrace:

Application: w3wp.exe CoreCLR Version: 8.0.1825.31117 .NET Version: 8.0.18 Description: The application requested process termination through System.Environment.FailFast. Message: A callback was made on a garbage collected delegate of type 'PDFiumSharp!PDFiumSharp.Types.FileReadBlockHandler::Invoke'. Stack: at PDFiumSharp.PDFium+PlatformInvoke.FPDF_LoadPage_x64(PDFiumSharp.Types.FPDF_DOCUMENT, Int32) at PDFiumSharp.PDFium+PlatformInvoke.FPDF_LoadPage_x64(PDFiumSharp.Types.FPDF_DOCUMENT, Int32) at PDFiumSharp.PDFium.FPDF_LoadPage(PDFiumSharp.Types.FPDF_DOCUMENT, Int32) at PDFiumSharp.PdfPage.Load(PDFiumSharp.PdfDocument, Int32) at PDFiumSharp.PdfPageCollection.get_Item(Int32) at PDFiumSharp.PdfPageCollection+<System-Collections-Generic-IEnumerable<PDFiumSharp-PdfPage>-GetEnumerator>d__8.MoveNext()

PDFiumSharpV2 V1.1.4

EddyRts avatar Jul 16 '25 16:07 EddyRts