[gl2ps] Memory Leak

Christophe Geuzaine cag32 at case.edu
Thu Nov 9 05:16:49 CET 2006


Jason Anderssen wrote:
> Hi,
> 
> Firstly, I am new to this list, and wish to extend an appreciatiion to 
> the great job all has done.  Anyhow, down to business.
> I think I have found a memory leak (well it leaked for me anyway), and 
> have fixed it.  Here is the code that I changed : SEE ( "Fixed memory 
> leak J.A" below for my changes ).

Hi Jason - Indeed, looks looks like a memory leak to me too. And I 
cannot remember why we need to make a deep copy of the primitives in 
this pdfprimlist in the first place... Micha, any insights?

Thanks for the bug report!

Christophe



> 
> static void gl2psPrintPDFFooter(void)
> {
>     GL2PSprimitive *p = NULL;
>   int i, offs; 
> 
>   gl2psPDFgroupListInit();
>   gl2psPDFgroupListWriteMainStream();
>  
>   offs = gl2ps->xreflist[5] + gl2ps->streamlength;
>   offs += gl2psClosePDFDataStream();
>   gl2ps->xreflist[5] = offs;
>  
>   offs += gl2psPrintPDFDataStreamLength(gl2ps->streamlength);
>   gl2ps->xreflist[6] = offs;
>   gl2ps->streamlength = 0;
>  
>   offs += gl2psPrintPDFOpenPage();
>   offs += gl2psPDFgroupListWriteVariableResources();
>   gl2ps->xreflist = (int*)gl2psRealloc(gl2ps->xreflist,
>                                        sizeof(int) * 
> (gl2ps->objects_stack + 1));
>   gl2ps->xreflist[7] = offs;
>  
>   offs += gl2psPrintPDFGSObject();
>   gl2ps->xreflist[8] = offs;
>  
>   gl2ps->xreflist[gl2ps->objects_stack] =
>     gl2psPDFgroupListWriteObjects(gl2ps->xreflist[8]);
> 
>   /* Start cross reference table. The file has to been opened in
>      binary mode to preserve the 20 digit string length! */
>   fprintf(gl2ps->stream,
>           "xref\n"
>           "0 %d\n"
>           "%010d 65535 f \n", gl2ps->objects_stack, 0);
>  
>   for(i = 1; i < gl2ps->objects_stack; ++i)
>     fprintf(gl2ps->stream, "%010d 00000 n \n", gl2ps->xreflist[i]);
>  
>   fprintf(gl2ps->stream,
>           "trailer\n"
>           "<<\n"
>           "/Size %d\n"
>           "/Info 1 0 R\n"
>           "/Root 2 0 R\n"
>           ">>\n"
>           "startxref\n%d\n"
>           "%%%%EOF\n",
>           gl2ps->objects_stack, gl2ps->xreflist[gl2ps->objects_stack]);
>  
>   /* Free auxiliary lists and arrays */   
>   gl2psFree(gl2ps->xreflist);
> 
>   /* Fixed memory leak J.A */
>   for(i = 0; i < gl2psListNbr(gl2ps->pdfprimlist); ++i){ 
>     p = *(GL2PSprimitive**)gl2psListPointer(gl2ps->pdfprimlist, i);
>     gl2psFreePrimitive(&p);
>   }
> 
>   gl2psListDelete(gl2ps->pdfprimlist);
>   gl2psPDFgroupListDelete();
>  
> #if defined(GL2PS_HAVE_ZLIB)
>   if(gl2ps->options & GL2PS_COMPRESS){
>     gl2psFreeCompress();
>     gl2psFree(gl2ps->compress);
>     gl2ps->compress = NULL;
>   }
> #endif
> }
> 
> Anyhow, please let me know if this was the correct thing to do, as I do 
> not know the code very well, this was a just a quick find, and thought I 
> would forward what I had found.
> 
> Cheers
> Jason
> 
> _______________________________________________
> gl2ps mailing list
> gl2ps at geuz.org
> http://www.geuz.org/mailman/listinfo/gl2ps
> 


-- 
Christophe Geuzaine
Assistant Professor, Case Western Reserve University, Mathematics
http://www.case.edu/artsci/math/geuzaine