shithub: jbig2

Download patch

ref: 81b746740b074f5a0a8b539bc4954dad5c5797be
parent: 304420e16947853c697c9f50eb9e7c3cdd64f80c
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Mon Jan 3 23:53:16 EST 2005

Properly free some segment results when the segment header structures 
are freed. Symbol dictionaries are released, intermediate segment images 
are released if they haven't already been released and nulled after 
their use in the decode process, and metadata structures are properly 
freed.

Resolves all known leaks with the first 7 ubc test streams.


git-svn-id: http://svn.ghostscript.com/jbig2dec/trunk@384 ded80894-8fb9-0310-811b-c03f3676ab4d

--- a/jbig2.c
+++ b/jbig2.c
@@ -1,7 +1,7 @@
 /*
     jbig2dec
     
-    Copyright (c) 2002-2003 artofcode LLC.
+    Copyright (c) 2002-2005 artofcode LLC.
     
     This software is provided AS-IS with no warranty,
     either express or implied.
@@ -353,7 +353,7 @@
 
   jbig2_free(ca, ctx->buf);
   if (ctx->segments != NULL) {
-    for (i = ctx->segment_index; i < ctx->n_segments; i++)
+    for (i = 0; i < ctx->n_segments; i++)
       jbig2_free_segment(ctx, ctx->segments[i]);
     jbig2_free(ca, ctx->segments);
   }
--- a/jbig2_refinement.c
+++ b/jbig2_refinement.c
@@ -319,7 +319,7 @@
 }
 
 /** 
- * Handler for immediate generic region segments
+ * Handler for generic refinement region segments
  */
 int
 jbig2_refinement_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
--- a/jbig2_segment.c
+++ b/jbig2_segment.c
@@ -129,7 +129,24 @@
   if (segment->referred_to_segments != NULL) {
     jbig2_free(ctx->allocator, segment->referred_to_segments);
   }
-  /* todo: free result */
+  /* todo: we need either some separate fields or
+     a more complex result object rather than this
+     brittle special casing */
+    switch (segment->flags & 63) {
+	case 0:  /* symbol dictionary */
+	  jbig2_sd_release(ctx, segment->result);
+	case 4:  /* intermediate text region */
+	case 40: /* intermediate refinement region */
+	  if (segment->result != NULL)
+	    jbig2_image_release(ctx, segment->result);
+	  break;
+	case 62:
+	  jbig2_metadata_free(ctx, segment->result);
+	  break;
+	default:
+	  /* anything is is probably an undefined pointer */
+	  break;
+    }
   jbig2_free (ctx->allocator, segment);
 }