Memory leak in TiffAppendPageFromGdPictureImage?

Discussions about image processing and document imaging.
Post Reply
MarkoZ
Posts: 19
Joined: Tue Jun 29, 2010 9:30 am

Memory leak in TiffAppendPageFromGdPictureImage?

Post by MarkoZ » Tue May 29, 2012 1:21 pm

Hi,

I'm using latest version of GdPicture. This is the code that's causing problems.

Code: Select all

        private void ConvertToTiff(byte[] dokument)
        {
            GdPicturePDF gdPdf = new GdPicturePDF();
            GdPictureImaging gdImaging = new GdPictureImaging();
            gdPdf.LoadFromStream(new MemoryStream(dokument));

            int multipageTifID = 0;
            for (int pageNr = 1; pageNr <= gdPdf.GetPageCount(); pageNr++)
            {
                gdPdf.SelectPage(pageNr);
                int pageImageID = gdPdf.RenderPageToGdPictureImageEx(100, false, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
                if (pageImageID == 0)
                {
                    throw new Exception("Napaka v GdPictureImaging: " + gdPdf.GetStat().ToString());
                }

                if (multipageTifID == 0)
                {
                    multipageTifID = gdImaging.TiffCreateMultiPageFromGdPictureImage(pageImageID);
                    if (multipageTifID == 0)
                    {
                        throw new Exception("Napaka v GdPictureImaging: " + gdImaging.GetStat().ToString());
                    }
                }
                else
                {
                    GdPictureStatus status = gdImaging.TiffAppendPageFromGdPictureImage(multipageTifID, pageImageID);
                    if (status != GdPictureStatus.OK)
                    {
                        throw new Exception("Napaka v GdPictureImaging: " + status.ToString());
                    }
                }

                gdImaging.ReleaseGdPictureImage(pageImageID);
            }
            gdImaging.ReleaseGdPictureImage(multipageTifID);
            gdPdf.CloseDocument();
        }
Even after releasing entire generated tiff image GdPicture is still holding some memory. Only way to release it is ClearGdPicture function, but that also kills anything else still in use elsewhere, so that's not a viable solution.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Memory leak in TiffAppendPageFromGdPictureImage?

Post by Loïc » Wed May 30, 2012 11:33 am

Hello,

The only memory leak I can find in your code is the non disposed MemoryStream. Despite the Garbage Collector can take care to invoke the Dispose method, I suggest you to call it by yourself when you are done with the object. Or simply use the using statement like this:

Code: Select all

            using (MemoryStream ms = new MemoryStream(dokument))
            {
                gdPdf.LoadFromStream(ms);

                int multipageTifID = 0;
                for (int pageNr = 1; pageNr <= gdPdf.GetPageCount(); pageNr++)
                {
                    gdPdf.SelectPage(pageNr);
                    int pageImageID = gdPdf.RenderPageToGdPictureImageEx(100, false, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
                    if (pageImageID == 0)
                    {
                        throw new Exception("Napaka v GdPictureImaging: " + gdPdf.GetStat().ToString());
                    }

                    if (multipageTifID == 0)
                    {
                        multipageTifID = gdImaging.TiffCreateMultiPageFromGdPictureImage(pageImageID);
                        if (multipageTifID == 0)
                        {
                            throw new Exception("Napaka v GdPictureImaging: " + gdImaging.GetStat().ToString());
                        }
                    }
                    else
                    {
                        GdPictureStatus status = gdImaging.TiffAppendPageFromGdPictureImage(multipageTifID, pageImageID);
                        if (status != GdPictureStatus.OK)
                        {
                            throw new Exception("Napaka v GdPictureImaging: " + status.ToString());
                        }
                    }

                    gdImaging.ReleaseGdPictureImage(pageImageID);
                }
                gdImaging.ReleaseGdPictureImage(multipageTifID);
                gdPdf.CloseDocument();
            }

MarkoZ
Posts: 19
Joined: Tue Jun 29, 2010 9:30 am

Re: Memory leak in TiffAppendPageFromGdPictureImage?

Post by MarkoZ » Wed May 30, 2012 12:34 pm

Hello,

I've just tried disposing memory stream manually, but nothing changed. When converting 700 page PDF (~6 MB in size) document, application still holds 300 MB of memory when function returns.

Only way, that I've found, to release that memory is ClearGdPicture function. I'm fairly certain that TiffAppendPageFromGdPictureImage is the cause of this problem, because if I comment it out, problem disappears.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Memory leak in TiffAppendPageFromGdPictureImage?

Post by Loïc » Thu May 31, 2012 10:52 pm

Hello,
When converting 700 page PDF (~6 MB in size) document, application still holds 300 MB of memory when function returns.
OK. If you start 10 time the same process do you have 3000 of memory used ?

If yes, we have a memory leak in GdPicture.NET. If no, this is a typical garbage collector behavior. Just call the GC.Collect() to recover the unused memory.

Kind regards,

Loïc

MarkoZ
Posts: 19
Joined: Tue Jun 29, 2010 9:30 am

Re: Memory leak in TiffAppendPageFromGdPictureImage?

Post by MarkoZ » Fri Jun 01, 2012 8:12 am

Hello,

calling GC.Collect() did release the memory. Thanks for your help.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest