* Inserted tool for memory leaks fixing in debug mode
authorAntonin Descampe <antonin@gmail.com>
Fri, 6 Aug 2004 14:13:51 +0000 (14:13 +0000)
committerAntonin Descampe <antonin@gmail.com>
Fri, 6 Aug 2004 14:13:51 +0000 (14:13 +0000)
* Fixed some memory leaks in the decoder (some are still present when multiple tiles!)

codec/image_to_j2k.c
codec/j2k_to_image.c
libopenjpeg/j2k.c
libopenjpeg/j2k.h
libopenjpeg/tcd.c
libopenjpeg/tcd.h

index 54bd0c92c8ad06b0d6dfb8fc785f92ced491a14b..e704702659b211fc009f89067de6ad2a1a3fef27 100644 (file)
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+//MEMORY LEAK
+#ifdef _DEBUG
+#define _CRTDBG_MAP_ALLOC
+#include <stdlib.h>  // Must be included first
+#include <crtdbg.h>
+#endif
+//MEM
+
 #include <openjpeg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -895,5 +903,12 @@ int main(int argc, char **argv)
     free(cp.tcps[tileno].tccps);
   free(cp.tcps);
 
+
+  //MEMORY LEAK
+  #ifdef _DEBUG
+    _CrtDumpMemoryLeaks();
+  #endif
+  //MEM
+
   return 0;
 }
index 531047366ab90f66e633fefedbc070cab517a91b..f1534d5379baa0df5bfdf811489d78a3a00d1c23 100644 (file)
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+//MEMORY LEAK
+#ifdef _DEBUG
+#define _CRTDBG_MAP_ALLOC
+#include <stdlib.h>  // Must be included first
+#include <crtdbg.h>
+#endif
+//MEM
+
 
 
 #include <openjpeg.h>
@@ -39,9 +47,9 @@ int ceildiv(int a, int b)
 
 int main(int argc, char **argv)
 {
-  FILE *f;
-  char *src, *src_name;
-  char *dest, S1, S2, S3;
+  FILE *f=NULL;
+  char *src=NULL, *src_name=NULL;
+  char *dest=NULL, S1, S2, S3;
   int len;
 
   j2k_image_t img;
@@ -50,7 +58,7 @@ int main(int argc, char **argv)
   int w, wr, wrr, h, hr, hrr, max;
   int i, image_type = -1, compno, pad, j;
   int adjust;
-  jp2_struct_t *jp2_struct;
+  jp2_struct_t *jp2_struct=NULL;
 
   if (argc < 3) {
     fprintf(stderr,
@@ -553,5 +561,13 @@ int main(int argc, char **argv)
     break;
   }
 
+  j2k_dec_release();
+
+  //MEMORY LEAK
+  #ifdef _DEBUG
+    _CrtDumpMemoryLeaks();
+  #endif
+  //MEM
+
   return 0;
 }
index e8f766e428c014ed3599b81e12277be9cbad04f6..6c611adf7a26af0bd4ddfd7094627bd3fdead698 100644 (file)
@@ -1458,6 +1458,7 @@ j2k_decode(unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp)
   cio_init(src, len);
 
   for (;;) {
+
     j2k_dec_mstabent_t *e;
     int id = cio_read(2);
     if (id >> 8 != 0xff) {
@@ -1473,6 +1474,7 @@ j2k_decode(unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp)
     if (e->handler) {
       (*e->handler) ();
     }
+
     if (j2k_state == J2K_STATE_NEOC)
       break;                   /* RAJOUTE */
   }
@@ -1562,6 +1564,26 @@ j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t * img,
   return 0;
 }
 
+void j2k_dec_release()
+{
+  int i=0;
+
+  //tcd_dec_release();
+
+  if (j2k_tile_len!=NULL) free(j2k_tile_len);
+  if (j2k_tile_data!=NULL) free(j2k_tile_data);
+  if (j2k_default_tcp.ppt_data!=NULL) free(j2k_default_tcp.ppt_data);
+  if (j2k_default_tcp.tccps!=NULL) free(j2k_default_tcp.tccps);
+  for (i=0;i<j2k_cp->tw*j2k_cp->th;i++) {
+    if (j2k_cp->tcps[i].ppt_data!=NULL) free(j2k_cp->tcps[i].ppt_data);
+    if (j2k_cp->tcps[i].tccps!=NULL) free(j2k_cp->tcps[i].tccps);
+  }
+  if (j2k_cp->ppm_data!=NULL) free(j2k_cp->ppm_data);
+  if (j2k_cp->tcps!=NULL) free(j2k_cp->tcps);
+  if (j2k_img->comps!=NULL) free(j2k_img->comps);
+  if (j2k_cp->tileno!=NULL) free(j2k_cp->tileno);
+}
+
 #ifdef WIN32
 #include <windows.h>
 
index aafa9de6dc7c511a4438849e1b56e180ece8c15a..7df8ecc7bbb2b724460ffaea5a6eaa137d651f35 100644 (file)
@@ -220,4 +220,6 @@ LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t * img,
 int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t * img,
                          j2k_cp_t * cp);
 
+void j2k_dec_release();//antonin
+
 #endif
index ab71c1295c5290a1307523460d24b1829ca4009c..ff9e0c95c2567defa329e453a6b5b183bf6adbea 100644 (file)
@@ -1614,6 +1614,7 @@ int tcd_decode_tile(unsigned char *src, int len, int tileno)
   fprintf(stderr, "total:     %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
          (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
 
+
   for (compno = 0; compno < tile->numcomps; compno++) {
     free(tcd_image.tiles[tileno].comps[compno].data);
   }
@@ -1624,3 +1625,30 @@ int tcd_decode_tile(unsigned char *src, int len, int tileno)
 
   return l;
 }
+
+void tcd_dec_release()
+{
+  int tileno,compno,resno,bandno,precno;
+  for (tileno=0;tileno<tcd_image.tw*tcd_image.th;tileno++) {
+    tcd_tile_t tile=tcd_image.tiles[tileno];
+    for (compno=0;compno<tile.numcomps;compno++) {
+      tcd_tilecomp_t tilec=tile.comps[compno];
+      for (resno=0;resno<tilec.numresolutions;resno++) {
+       tcd_resolution_t res=tilec.resolutions[resno];
+       for (bandno=0;bandno<res.numbands;bandno++) {
+         tcd_band_t band=res.bands[bandno];
+         for (precno=0;precno<res.ph*res.pw;precno++) {
+           tcd_precinct_t prec=band.precincts[precno];
+           if (prec.cblks!=NULL) free(prec.cblks);
+           if (prec.imsbtree!=NULL) free(prec.imsbtree);
+           if (prec.incltree!=NULL) free(prec.incltree);
+         }
+         if (band.precincts!=NULL) free(band.precincts);
+       }
+      }
+      if (tilec.resolutions!=NULL) free(tilec.resolutions);
+    }
+    if (tile.comps!=NULL) free(tile.comps);
+  }
+  if (tcd_image.tiles!=NULL) free(tcd_image.tiles);
+}
index 6e64717da3405ffccd98d2c76369b1dea4a00bf7..3201243d155d34b2f172222f9f52656df48dac21 100644 (file)
@@ -179,4 +179,6 @@ int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
  */
 int tcd_decode_tile(unsigned char *src, int len, int tileno);
 
+void tcd_dec_release();
+
 #endif