GDPicturePDF Memory Leak

Discussions about PDF management.
Post Reply
ElkayEQ
Posts: 16
Joined: Tue Oct 06, 2015 6:43 am

GDPicturePDF Memory Leak

Post by ElkayEQ » Fri May 27, 2016 2:24 am

I'm VERY certain I'm doing something wrong! The reason I know this is because the memory leak is so severe there's no way it would have gone unnoticed! ANY HELP would be very appreciated!

Here's the method:

Code: Select all

	Public Function VoidPDF() As Boolean


		Dim OldImageID As Integer
		Dim OldStatus As GdPictureStatus = Nothing
		Dim Failed As Boolean = False
		Dim Status As GdPictureStatus = Nothing

		Using NewPDF As New GdPicturePDF
			NewPDF.NewPDF()

			Using OldGDPicImage As New GdPictureImaging

				Using OldPDF As New GdPicturePDF
					Status = OldPDF.LoadFromFile(_OldFile, False)

					If Status = GdPictureStatus.OK Then

						OldPDF.SelectPage(1)
						OldImageID = OldPDF.RenderPageToGdPictureImage(300.0, False)
						If OldImageID <> 0 Then

							' Create an Image from the old PDF and write VOID on it
							Dim myImage As Image = OldGDPicImage.GetBitmapFromGdPictureImage(OldImageID)
							Dim g As Graphics = Graphics.FromImage(myImage)
							g.DrawString("VOID", _VoidFont, Brushes.DarkRed, New PointF(1100, 3480))
							g.Dispose()
							g = Nothing

							' Go create my new PDF
							Using NewGDPicImage As New GdPictureImaging
								Dim NewImageID As Integer = NewGDPicImage.CreateGdPictureImageFromBitmap(myImage)

								NewPDF.AddImageFromGdPictureImage(NewImageID, False, True)
								If NewPDF.GetStat() <> GdPictureStatus.OK Then
									NewPDF.CloseDocument()
									NewGDPicImage.ReleaseGdPictureImage(NewImageID)
									MsgBox("Failed to Add Image")
									Failed = True
								End If
								NewGDPicImage.ReleaseGdPictureImage(NewImageID)

							End Using ' NewGDPicImage
							GC.Collect()

						End If

					End If

				End Using ' OldPDF

			End Using ' OldGDPicImage

			If Not Failed Then
				NewPDF.SaveToFile(_NewFile)
				If NewPDF.GetStat() <> GdPictureStatus.OK Then
					MsgBox("Failed to Save New PDF")
				End If
			End If

			NewPDF.CloseDocument()
			NewPDF.Dispose()

		End Using ' NewPDF
		GC.Collect()

		Return Not Failed

	End Function
Here's what the function does:
1) Opens a single-page PDF (about 1 meg in size...these are 300dpi PDFs)
2) Creates a GDPIcture Image from that page.
3) I write some text on the image.
4) I create a new PDF using the new image.
5) Close everything down and clean up.

The resulting PDF is also roughly 1meg in size as expected...however, I'm inside a loop that's converting 10,000 images and every time this function fires, I lose about 1meg of ram. I must be doing something very wrong - if anyone can see anything that I've obviously missed, your comments would be very much appreciated.

Kindest regards,
Elkay.

David
Posts: 66
Joined: Mon Feb 08, 2016 3:12 pm

Re: GDPicturePDF Memory Leak

Post by David » Fri May 27, 2016 2:35 pm

Hi Elkay,

Thank you for your message.

The root of the issue is the image created with the call to RenderPageToGdPictureImage is never release:

OldImageID = OldPDF.RenderPageToGdPictureImage(300.0, False)

You should call ReleaseGdPictureImage with OldImageID before returning from the function.

Have a nice day,

David

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests