Index: GraphicsMagick-1.2.5/coders/pdf.c
===================================================================
--- GraphicsMagick-1.2.5.orig/coders/pdf.c	2019-02-11 18:11:59.422593339 +0100
+++ GraphicsMagick-1.2.5/coders/pdf.c	2019-02-11 18:17:37.972215830 +0100
@@ -1191,11 +1191,17 @@ static unsigned int WritePDFImage(const
                 */
                 jpeg_image=CloneImage(image,0,0,True,&image->exception);
                 if (jpeg_image == (Image *) NULL)
+                {
+                  MagickFreeMemory(xref);
                   ThrowWriterException2(CoderError,image->exception.reason,image);
+                }
                 (void) strcpy(jpeg_image->magick,"JPEG");
                 blob=ImageToBlob(image_info,jpeg_image,&length,&image->exception);
                 if (blob == (void *) NULL)
+                {
+                  MagickFreeMemory(xref);
                   ThrowWriterException2(CoderError,image->exception.reason,image);
+                }
                 (void) WriteBlob(image,length,blob);
                 DestroyImage(jpeg_image);
                 MagickFreeMemory(blob);
@@ -1252,6 +1258,7 @@ static unsigned int WritePDFImage(const
                 if (!status)
                   {
                     CloseBlob(image);
+                    MagickFreeMemory(xref);
                     return(False);
                   }
                 break;
@@ -1309,11 +1316,17 @@ static unsigned int WritePDFImage(const
                 */
                 jpeg_image=CloneImage(image,0,0,True,&image->exception);
                 if (jpeg_image == (Image *) NULL)
+                {
+                  MagickFreeMemory(xref);
                   ThrowWriterException2(CoderError,image->exception.reason,image);
+                }
                 (void) strcpy(jpeg_image->magick,"JPEG");
                 blob=ImageToBlob(image_info,jpeg_image,&length,&image->exception);
                 if (blob == (void *) NULL)
+                {
+                  MagickFreeMemory(xref);
                   ThrowWriterException2(CoderError,image->exception.reason,image);
+                }
                 (void) WriteBlob(image,length,blob);
                 DestroyImage(jpeg_image);
                 MagickFreeMemory(blob);
@@ -1382,6 +1395,7 @@ static unsigned int WritePDFImage(const
                 if (!status)
                   {
                     CloseBlob(image);
+                    MagickFreeMemory(xref);
                     return(False);
                   }
                 break;
@@ -1482,6 +1496,7 @@ static unsigned int WritePDFImage(const
                   if (!status)
                     {
                       CloseBlob(image);
+                      MagickFreeMemory(xref);
                       return(False);
                     }
                   break;
@@ -1571,7 +1586,10 @@ static unsigned int WritePDFImage(const
           tile_image=ThumbnailImage(image,geometry.width,geometry.height,
                                     &image->exception);
           if (tile_image == (Image *) NULL)
+          {
+            MagickFreeMemory(xref);
             ThrowWriterException2(ResourceLimitError,image->exception.reason,image);
+          }
           xref[object++]=TellBlob(image);
           FormatString(buffer,"%lu 0 obj\n",object);
           (void) WriteBlobString(image,buffer);
@@ -1667,11 +1685,17 @@ static unsigned int WritePDFImage(const
                     */
                     jpeg_image=CloneImage(tile_image,0,0,True,&image->exception);
                     if (jpeg_image == (Image *) NULL)
+                    {
+                      MagickFreeMemory(xref);
                       ThrowWriterException2(CoderError,image->exception.reason,image);
+                    }
                     (void) strcpy(jpeg_image->magick,"JPEG");
                     blob=ImageToBlob(image_info,jpeg_image,&length,&image->exception);
                     if (blob == (void *) NULL)
+                    {
+                      MagickFreeMemory(xref);
                       ThrowWriterException2(CoderError,image->exception.reason,image);
+                    }
                     (void) WriteBlob(image,length,blob);
                     DestroyImage(jpeg_image);
                     MagickFreeMemory(blob);
@@ -1719,6 +1743,7 @@ static unsigned int WritePDFImage(const
                     if (!status)
                       {
                         CloseBlob(image);
+                        MagickFreeMemory(xref);
                         return(False);
                       }
                     break;
@@ -1768,11 +1793,17 @@ static unsigned int WritePDFImage(const
                     */
                     jpeg_image=CloneImage(tile_image,0,0,True,&image->exception);
                     if (jpeg_image == (Image *) NULL)
+                    {
+                      MagickFreeMemory(xref);
                       ThrowWriterException2(CoderError,image->exception.reason,image);
+                    }
                     (void) strcpy(jpeg_image->magick,"JPEG");
                     blob=ImageToBlob(image_info,jpeg_image,&length,&image->exception);
                     if (blob == (void *) NULL)
+                    {
+                      MagickFreeMemory(xref);
                       ThrowWriterException2(CoderError,image->exception.reason,image);
+                    }
                     (void) WriteBlob(image,length,blob);
                     DestroyImage(jpeg_image);
                     MagickFreeMemory(blob);
@@ -1832,6 +1863,7 @@ static unsigned int WritePDFImage(const
                     if (!status)
                       {
                         CloseBlob(image);
+                        MagickFreeMemory(xref);
                         return(False);
                       }
                     break;
@@ -1916,6 +1948,7 @@ static unsigned int WritePDFImage(const
                       if (!status)
                         {
                           CloseBlob(image);
+                          MagickFreeMemory(xref);
                           return(False);
                         }
                       break;
