From c5f91f9fa29922713d3751654dda09ccc8518801 Mon Sep 17 00:00:00 2001 From: Ken Sharp Date: Sat, 5 May 2012 11:21:31 +0100 Subject: pdfwrite - memory leaks with CIDFonts Move the code to free font resources from the 'pdf close' into a separate routine in the font handling module. Additionally call it for CIDFont resoruces as well as Font resources (the code copes with freeing both) No differences expected --- gs/base/gdevpdf.c | 87 ++++++++++-------------------------------------------- gs/base/gdevpdtf.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++ gs/base/gdevpdtf.h | 2 ++ 3 files changed, 92 insertions(+), 71 deletions(-) diff --git a/gs/base/gdevpdf.c b/gs/base/gdevpdf.c index 28982f402..cdd2f2a8e 100644 --- a/gs/base/gdevpdf.c +++ b/gs/base/gdevpdf.c @@ -1501,77 +1501,22 @@ pdf_close(gx_device * dev) for (; pres != 0;) { pdf_font_resource_t *pdfont = (pdf_font_resource_t *)pres; - if(pdfont->BaseFont.size) { - gs_free_string(pdev->memory, pdfont->BaseFont.data, pdfont->BaseFont.size, "Free BaseFont string"); - pdfont->BaseFont.data = (byte *)0L; - pdfont->BaseFont.size = 0; - } - if(pdfont->Widths) { - gs_free_object(pdev->memory, pdfont->Widths, "Free Widths array"); - pdfont->Widths = 0; - } - if(pdfont->used) { - gs_free_object(pdev->memory, pdfont->used, "Free used array"); - pdfont->used = 0; - } - if(pdfont->res_ToUnicode) { - /* ToUnicode resources are released below */ -/* gs_free_object(pdev->memory, pdfont->res_ToUnicode, "Free ToUnicode resource");*/ - pdfont->res_ToUnicode = 0; - } - if(pdfont->cmap_ToUnicode) { - gs_cmap_ToUnicode_free(pdev->memory, pdfont->cmap_ToUnicode); - pdfont->cmap_ToUnicode = 0; - } - switch(pdfont->FontType) { - case ft_composite: - break; - case ft_PCL_user_defined: - case ft_GL2_stick_user_defined: - case ft_user_defined: - if(pdfont->u.simple.Encoding) { - gs_free_object(pdev->memory, pdfont->u.simple.Encoding, "Free simple Encoding"); - pdfont->u.simple.Encoding = 0; - } - if(pdfont->u.simple.v) { - gs_free_object(pdev->memory, pdfont->u.simple.v, "Free simple v"); - pdfont->u.simple.v = 0; - } - if (pdfont->u.simple.s.type3.char_procs) { - pdf_free_charproc_ownership(pdev, (pdf_resource_t *)pdfont->u.simple.s.type3.char_procs); - pdfont->u.simple.s.type3.char_procs = 0; - } - break; - case ft_CID_encrypted: - case ft_CID_TrueType: - if(pdfont->u.cidfont.used2) { - gs_free_object(pdev->memory, pdfont->u.cidfont.used2, "Free CIDFont used2"); - pdfont->u.cidfont.used2 = 0; - } - if(pdfont->u.cidfont.CIDToGIDMap) { - gs_free_object(pdev->memory, pdfont->u.cidfont.CIDToGIDMap, "Free CIDToGID map"); - pdfont->u.cidfont.CIDToGIDMap = 0; - } - break; - default: - if(pdfont->u.simple.Encoding) { - gs_free_object(pdev->memory, pdfont->u.simple.Encoding, "Free simple Encoding"); - pdfont->u.simple.Encoding = 0; - } - if(pdfont->u.simple.v) { - gs_free_object(pdev->memory, pdfont->u.simple.v, "Free simple v"); - pdfont->u.simple.v = 0; - } - break; - } - if (pdfont->object) { - cos_release(pdfont->object, "release font resource object"); - gs_free_object(pdev->pdf_memory, pdfont->object, "Free font resource object"); - pdfont->object = 0; - } - /* We free FontDescriptor resources separately */ - if(pdfont->FontDescriptor) - pdfont->FontDescriptor = NULL; + font_resource_free(pdev, pdfont); + pres = pres->next; + } + } + } + + { + int j, code = 0; + + for (j = 0; j < NUM_RESOURCE_CHAINS && code >= 0; ++j) { + pdf_resource_t *pres = pdev->resources[resourceCIDFont].chains[j]; + + for (; pres != 0;) { + pdf_font_resource_t *pdfont = (pdf_font_resource_t *)pres; + + font_resource_free(pdev, pdfont); pres = pres->next; } } diff --git a/gs/base/gdevpdtf.c b/gs/base/gdevpdtf.c index 5b1c49d31..09cfa2419 100644 --- a/gs/base/gdevpdtf.c +++ b/gs/base/gdevpdtf.c @@ -405,6 +405,80 @@ font_resource_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres, return code; } +int font_resource_free(gx_device_pdf *pdev, pdf_font_resource_t *pdfont) +{ + if(pdfont->BaseFont.size) { + gs_free_string(pdev->memory, pdfont->BaseFont.data, pdfont->BaseFont.size, "Free BaseFont string"); + pdfont->BaseFont.data = (byte *)0L; + pdfont->BaseFont.size = 0; + } + if(pdfont->Widths) { + gs_free_object(pdev->memory, pdfont->Widths, "Free Widths array"); + pdfont->Widths = 0; + } + if(pdfont->used) { + gs_free_object(pdev->memory, pdfont->used, "Free used array"); + pdfont->used = 0; + } + if(pdfont->res_ToUnicode) { + /* ToUnicode resources are tracked amd released separately */ + pdfont->res_ToUnicode = 0; + } + if(pdfont->cmap_ToUnicode) { + gs_cmap_ToUnicode_free(pdev->memory, pdfont->cmap_ToUnicode); + pdfont->cmap_ToUnicode = 0; + } + switch(pdfont->FontType) { + case ft_composite: + break; + case ft_PCL_user_defined: + case ft_GL2_stick_user_defined: + case ft_user_defined: + if(pdfont->u.simple.Encoding) { + gs_free_object(pdev->memory, pdfont->u.simple.Encoding, "Free simple Encoding"); + pdfont->u.simple.Encoding = 0; + } + if(pdfont->u.simple.v) { + gs_free_object(pdev->memory, pdfont->u.simple.v, "Free simple v"); + pdfont->u.simple.v = 0; + } + if (pdfont->u.simple.s.type3.char_procs) { + pdf_free_charproc_ownership(pdev, (pdf_resource_t *)pdfont->u.simple.s.type3.char_procs); + pdfont->u.simple.s.type3.char_procs = 0; + } + break; + case ft_CID_encrypted: + case ft_CID_TrueType: + if(pdfont->u.cidfont.used2) { + gs_free_object(pdev->memory, pdfont->u.cidfont.used2, "Free CIDFont used2"); + pdfont->u.cidfont.used2 = 0; + } + if(pdfont->u.cidfont.CIDToGIDMap) { + gs_free_object(pdev->memory, pdfont->u.cidfont.CIDToGIDMap, "Free CIDToGID map"); + pdfont->u.cidfont.CIDToGIDMap = 0; + } + break; + default: + if(pdfont->u.simple.Encoding) { + gs_free_object(pdev->memory, pdfont->u.simple.Encoding, "Free simple Encoding"); + pdfont->u.simple.Encoding = 0; + } + if(pdfont->u.simple.v) { + gs_free_object(pdev->memory, pdfont->u.simple.v, "Free simple v"); + pdfont->u.simple.v = 0; + } + break; + } + if (pdfont->object) { + gs_free_object(pdev->pdf_memory, pdfont->object, "Free font resource object"); + pdfont->object = 0; + } + /* We free FontDescriptor resources separately */ + if(pdfont->FontDescriptor) + pdfont->FontDescriptor = NULL; + return 0; +} + int pdf_assign_font_object_id(gx_device_pdf *pdev, pdf_font_resource_t *pdfont) { diff --git a/gs/base/gdevpdtf.h b/gs/base/gdevpdtf.h index 4785b1a8b..c296e6c10 100644 --- a/gs/base/gdevpdtf.h +++ b/gs/base/gdevpdtf.h @@ -393,6 +393,8 @@ int font_resource_encoded_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfre pdf_font_write_contents_proc_t write_contents); int pdf_assign_font_object_id(gx_device_pdf *pdev, pdf_font_resource_t *pdfont); +int font_resource_free(gx_device_pdf *pdev, pdf_font_resource_t *pdfont); + /* Resize font resource arrays. */ int pdf_resize_resource_arrays(gx_device_pdf *pdev, pdf_font_resource_t *pfres, int chars_count); -- cgit v1.2.3