[gl2ps] Memory Leak

Jason Anderssen janderssen at exactal.com
Tue Nov 7 06:43:39 CET 2006


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 ).

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