[DVDnav-discuss] Removing ifoFree functions

Steve Dibb beandog at gentoo.org
Mon Jan 5 01:38:13 CET 2015


Alright, so the real problem is that there are all these ifoFree_*()
functions that are only called once by ifoFree().  Some of them call
other internal ifoFree functions, which gets even more confusing.  So
instead of doing all of that, I just moved it all to ifoFree() since
there's so little that happens in each function anyway.

The last major problem is fixing the code that cleans up the program
chains.  They are the ones that cause segfaults for me.  For example,
from ifoFreePGC, it starts with:

if(pgc && *pgc && (--(*pgc)->ref_count) <= 0) {

So, that needs to be properly fixed and the cleanup functions will be
simplified.  ifoClose() is all that the library really needs to hand
off the application anyway, since there's so many checks internally to
see if things are set or not.

Before you ask, the reason for all the if statements in the ifoClose is
because it doesn't check to see if it's the VMG or a VTS, so it looks
individually to see what's been set.  The library could probably use a
function to see which one the IFO is, though by specification, it
should just be 0 for the VMG and 1 through 99 for the VTS, but
whatever.  That's another discussion.

Since I'm not a git wizard, here is a simple diff for now.  Compiles
for me, works fine on all my DVDs I have ISOs of right now.

diff --git a/src/dvdread/ifo_read.h b/src/dvdread/ifo_read.h
index 97f4179..e298732 100644
--- a/src/dvdread/ifo_read.h
+++ b/src/dvdread/ifo_read.h
@@ -209,19 +209,10 @@ int ifoRead_TXTDT_MGI(ifo_handle_t *);
  * below are safe:  they will not mind if you attempt to free part of
an IFO
  * file which was not read in or which does not exist.
  */
-void ifoFree_PTL_MAIT(ifo_handle_t *);
-void ifoFree_VTS_ATRT(ifo_handle_t *);
-void ifoFree_TT_SRPT(ifo_handle_t *);
-void ifoFree_VTS_PTT_SRPT(ifo_handle_t *);
-void ifoFree_FP_PGC(ifo_handle_t *);
 void ifoFree_PGCIT(ifo_handle_t *);
-void ifoFree_PGCI_UT(ifo_handle_t *);
+void ifoFree_TT_SRPT(ifo_handle_t *);
 void ifoFree_VTS_TMAPT(ifo_handle_t *);
-void ifoFree_C_ADT(ifo_handle_t *);
-void ifoFree_TITLE_C_ADT(ifo_handle_t *);
-void ifoFree_VOBU_ADMAP(ifo_handle_t *);
-void ifoFree_TITLE_VOBU_ADMAP(ifo_handle_t *);
-void ifoFree_TXTDT_MGI(ifo_handle_t *);
+void ifoFree_PGCI_UT(ifo_handle_t *);
 
 #ifdef __cplusplus
 };
diff --git a/src/ifo_read.c b/src/ifo_read.c
index 807ebac..913e6ea 100644
--- a/src/ifo_read.c
+++ b/src/ifo_read.c
@@ -487,30 +487,93 @@ void ifoClose(ifo_handle_t *ifofile) {
   if(!ifofile)
     return;
 
-  ifoFree_VOBU_ADMAP(ifofile);
-  ifoFree_TITLE_VOBU_ADMAP(ifofile);
-  ifoFree_C_ADT(ifofile);
-  ifoFree_TITLE_C_ADT(ifofile);
-  ifoFree_TXTDT_MGI(ifofile);
-  ifoFree_VTS_ATRT(ifofile);
-  ifoFree_PTL_MAIT(ifofile);
-  ifoFree_PGCI_UT(ifofile);
-  ifoFree_TT_SRPT(ifofile);
-  ifoFree_FP_PGC(ifofile);
+  // ifoFree_VOBU_ADMAP(ifofile);
+  if(ifofile->menu_vobu_admap) {
+    free(ifofile->menu_vobu_admap->vobu_start_sectors);
+    free(ifofile->menu_vobu_admap);
+  }
+
+  // ifoFree_TITLE_VOBU_ADMAP(ifofile);
+  if(ifofile->vts_vobu_admap) {
+    free(ifofile->vts_vobu_admap->vobu_start_sectors);
+    free(ifofile->vts_vobu_admap);
+  }
+
+  // ifoFree_C_ADT(ifofile);
+  if(ifofile->menu_c_adt) {
+    free(ifofile->menu_c_adt->cell_adr_table);
+    free(ifofile->menu_c_adt);
+  }
+
+  // ifoFree_TITLE_C_ADT(ifofile);
+  if(ifofile->vts_c_adt) {
+    free(ifofile->vts_c_adt->cell_adr_table);
+    free(ifofile->vts_c_adt);
+  }
+
+  // ifoFree_TXTDT_MGI(ifofile);
+  if(ifofile->txtdt_mgi)
+    free(ifofile->txtdt_mgi);
+
+  // ifoFree_VTS_ATRT(ifofile);
+  if(ifofile->vts_atrt) {
+    free(ifofile->vts_atrt->vts);
+    free(ifofile->vts_atrt->vts_atrt_offsets);
+    free(ifofile->vts_atrt);
+  }
+
+  // ifoFree_PTL_MAIT(ifofile);
+  if(ifofile->ptl_mait) {
+    unsigned int i;
+
+    for(i = 0; i < ifofile->ptl_mait->nr_of_countries; i++) {
+      free(ifofile->ptl_mait->countries[i].pf_ptl_mai);
+    }
+    free(ifofile->ptl_mait->countries);
+    free(ifofile->ptl_mait);
+  }
+
+  // ifoFree_PGCI_UT(ifofile);
+  if(ifofile->pgci_ut) {
+    unsigned int i;
+
+    for(i = 0; i < ifofile->pgci_ut->nr_of_lus; i++) {
+      ifoFree_PGCIT_internal(&ifofile->pgci_ut->lu[i].pgcit);
+    }
+    free(ifofile->pgci_ut->lu);
+    free(ifofile->pgci_ut);
+  }
+
+  // ifoFree_TT_SRPT(ifofile);
+  if(ifofile->tt_srpt) {
+    free(ifofile->tt_srpt->title);
+    free(ifofile->tt_srpt);
+  }
+
+  // ifoFree_FP_PGC(ifofile);
+  if(ifofile->first_play_pgc)
+    ifoFree_PGC(&ifofile->first_play_pgc);
+
   ifoFree_PGCIT(ifofile);
-  ifoFree_VTS_PTT_SRPT(ifofile);
-  ifoFree_VTS_TMAPT(ifofile);
 
-  if(ifofile->vmgi_mat)
-    free(ifofile->vmgi_mat);
+  // ifoFree_VTS_PTT_SRPT(ifofile);
+  if(ifofile->vts_ptt_srpt) {
+    unsigned int i;
+    for(i = 0; i < ifofile->vts_ptt_srpt->nr_of_srpts; i++) {
+      free(ifofile->vts_ptt_srpt->title[i].ptt);
+    }
+    free(ifofile->vts_ptt_srpt->ttu_offset);
+    free(ifofile->vts_ptt_srpt->title);
+    free(ifofile->vts_ptt_srpt);
+  }
 
-  if(ifofile->vtsi_mat)
-    free(ifofile->vtsi_mat);
+  ifoFree_VTS_TMAPT(ifofile);
+
+  free(ifofile->vmgi_mat);
+  free(ifofile->vtsi_mat);
 
   DVDCloseFile(ifofile->file);
-  ifofile->file = 0;
   free(ifofile);
-  ifofile = 0;
 }
 
 
@@ -525,19 +588,19 @@ static int ifoRead_VMG(ifo_handle_t *ifofile) {
 
   if(!DVDFileSeek_(ifofile->file, 0)) {
     free(ifofile->vmgi_mat);
-    ifofile->vmgi_mat = 0;
+    ifofile->vmgi_mat = NULL;
     return 0;
   }
 
   if(!DVDReadBytes(ifofile->file, vmgi_mat, sizeof(vmgi_mat_t))) {
     free(ifofile->vmgi_mat);
-    ifofile->vmgi_mat = 0;
+    ifofile->vmgi_mat = NULL;
     return 0;
   }
 
   if(strncmp("DVDVIDEO-VMG", vmgi_mat->vmg_identifier, 12) != 0) {
     free(ifofile->vmgi_mat);
-    ifofile->vmgi_mat = 0;
+    ifofile->vmgi_mat = NULL;
     return 0;
   }
 
@@ -748,6 +811,7 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t
*ifofile, 
     if(!(DVDReadBytes(ifofile->file, cmd_tbl->pre_cmds,
pre_cmds_size))) { free(cmd_tbl->pre_cmds);
+      cmd_tbl->pre_cmds = NULL;
       return 0;
     }
   }
@@ -756,14 +820,15 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t
*ifofile, unsigned int post_cmds_size = cmd_tbl->nr_of_post *
COMMAND_DATA_SIZE; cmd_tbl->post_cmds = malloc(post_cmds_size);
     if(!cmd_tbl->post_cmds) {
-      if(cmd_tbl->pre_cmds)
-        free(cmd_tbl->pre_cmds);
+      free(cmd_tbl->pre_cmds);
+      cmd_tbl->pre_cmds = NULL;
       return 0;
     }
     if(!(DVDReadBytes(ifofile->file, cmd_tbl->post_cmds,
post_cmds_size))) {
-      if(cmd_tbl->pre_cmds)
-        free(cmd_tbl->pre_cmds);
+      free(cmd_tbl->pre_cmds);
+      cmd_tbl->pre_cmds = NULL;
       free(cmd_tbl->post_cmds);
+      cmd_tbl->post_cmds = NULL;
       return 0;
     }
   }
@@ -772,18 +837,19 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t
*ifofile, unsigned int cell_cmds_size = cmd_tbl->nr_of_cell *
COMMAND_DATA_SIZE; cmd_tbl->cell_cmds = malloc(cell_cmds_size);
     if(!cmd_tbl->cell_cmds) {
-      if(cmd_tbl->pre_cmds)
-        free(cmd_tbl->pre_cmds);
-      if(cmd_tbl->post_cmds)
-        free(cmd_tbl->post_cmds);
+      free(cmd_tbl->pre_cmds);
+      cmd_tbl->pre_cmds = NULL;
+      free(cmd_tbl->post_cmds);
+      cmd_tbl->post_cmds = NULL;
       return 0;
     }
     if(!(DVDReadBytes(ifofile->file, cmd_tbl->cell_cmds,
cell_cmds_size))) {
-      if(cmd_tbl->pre_cmds)
-        free(cmd_tbl->pre_cmds);
-      if(cmd_tbl->post_cmds)
-        free(cmd_tbl->post_cmds);
+      free(cmd_tbl->pre_cmds);
+      cmd_tbl->pre_cmds = NULL;
+      free(cmd_tbl->post_cmds);
+      cmd_tbl->post_cmds = NULL;
       free(cmd_tbl->cell_cmds);
+      cmd_tbl->cell_cmds = NULL;
       return 0;
     }
   }
@@ -797,13 +863,11 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t
*ifofile, 
 static void ifoFree_PGC_COMMAND_TBL(pgc_command_tbl_t *cmd_tbl) {
   if(cmd_tbl) {
-    if(cmd_tbl->nr_of_pre && cmd_tbl->pre_cmds)
-      free(cmd_tbl->pre_cmds);
-    if(cmd_tbl->nr_of_post && cmd_tbl->post_cmds)
-      free(cmd_tbl->post_cmds);
-    if(cmd_tbl->nr_of_cell && cmd_tbl->cell_cmds)
-      free(cmd_tbl->cell_cmds);
+    free(cmd_tbl->pre_cmds);
+    free(cmd_tbl->post_cmds);
+    free(cmd_tbl->cell_cmds);
     free(cmd_tbl);
+    cmd_tbl = NULL;
   }
 }
 
@@ -922,6 +986,8 @@ static int ifoRead_PGC(ifo_handle_t *ifofile, pgc_t
*pgc, unsigned int offset) { 
     if(!ifoRead_PGC_COMMAND_TBL(ifofile, pgc->command_tbl,
                                 offset + pgc->command_tbl_offset)) {
+      free(pgc->command_tbl);
+      pgc->command_tbl = NULL;
       return 0;
     }
   } else {
@@ -935,6 +1001,8 @@ static int ifoRead_PGC(ifo_handle_t *ifofile,
pgc_t *pgc, unsigned int offset) { }
     if(!ifoRead_PGC_PROGRAM_MAP(ifofile,
pgc->program_map,pgc->nr_of_programs, offset +
pgc->program_map_offset)) {
+      free(pgc->program_map);
+      pgc->program_map = NULL;
       return 0;
     }
   } else {
@@ -949,6 +1017,8 @@ static int ifoRead_PGC(ifo_handle_t *ifofile,
pgc_t *pgc, unsigned int offset)
{ if(!ifoRead_CELL_PLAYBACK_TBL(ifofile, pgc->cell_playback,
pgc->nr_of_cells, offset + pgc->cell_playback_offset)) {
+      free(pgc->cell_playback);
+      pgc->cell_playback = NULL;
       return 0;
     }
   } else {
@@ -963,6 +1033,8 @@ static int ifoRead_PGC(ifo_handle_t *ifofile,
pgc_t *pgc, unsigned int offset)
{ if(!ifoRead_CELL_POSITION_TBL(ifofile, pgc->cell_position,
pgc->nr_of_cells, offset + pgc->cell_position_offset)) {
+      free(pgc->cell_position);
+      pgc->cell_position = NULL;
       return 0;
     }
   } else {
@@ -1015,15 +1087,6 @@ static void ifoFree_PGC(pgc_t **pgc) {
   }
 }
 
-void ifoFree_FP_PGC(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->first_play_pgc) {
-    ifoFree_PGC(&ifofile->first_play_pgc);
-  }
-}
-
 
 int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
   tt_srpt_t *tt_srpt;
@@ -1051,6 +1114,7 @@ int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
   if(!(DVDReadBytes(ifofile->file, tt_srpt, TT_SRPT_SIZE))) {
     fprintf(stderr, "libdvdread: Unable to read read TT_SRPT.\n");
     free(tt_srpt);
+    ifofile->tt_srpt = NULL;
     return 0;
   }
 
@@ -1062,12 +1126,13 @@ int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
   tt_srpt->title = malloc(info_length);
   if(!tt_srpt->title) {
     free(tt_srpt);
-    ifofile->tt_srpt = 0;
+    ifofile->tt_srpt = NULL;
     return 0;
   }
   if(!(DVDReadBytes(ifofile->file, tt_srpt->title, info_length))) {
     fprintf(stderr, "libdvdread: Unable to read read TT_SRPT.\n");
-    ifoFree_TT_SRPT(ifofile);
+    free(ifofile->tt_srpt->title);
+    free(ifofile->tt_srpt);
     return 0;
   }
 
@@ -1121,18 +1186,6 @@ int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
 }
 
 
-void ifoFree_TT_SRPT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->tt_srpt) {
-    free(ifofile->tt_srpt->title);
-    free(ifofile->tt_srpt);
-    ifofile->tt_srpt = 0;
-  }
-}
-
-
 int ifoRead_VTS_PTT_SRPT(ifo_handle_t *ifofile) {
   vts_ptt_srpt_t *vts_ptt_srpt = NULL;
   int info_length, i, j;
@@ -1231,8 +1284,10 @@ int ifoRead_VTS_PTT_SRPT(ifo_handle_t *ifofile) {
     vts_ptt_srpt->title[i].nr_of_ptts = n / 4;
     vts_ptt_srpt->title[i].ptt = malloc(n * sizeof(ptt_info_t));
     if(!vts_ptt_srpt->title[i].ptt) {
-      for(n = 0; n < i; n++)
+      for(n = 0; n < i; n++) {
         free(vts_ptt_srpt->title[n].ptt);
+	vts_ptt_srpt->title[n].ptt = NULL;
+      }
 
       goto fail;
     }
@@ -1276,29 +1331,14 @@ int ifoRead_VTS_PTT_SRPT(ifo_handle_t *ifofile)
{ 
 fail:
   free(data);
-  ifofile->vts_ptt_srpt = 0;
+  ifofile->vts_ptt_srpt = NULL;
   free(vts_ptt_srpt->title);
+  vts_ptt_srpt->title = NULL;
   free(vts_ptt_srpt);
   return 0;
 }
 
 
-void ifoFree_VTS_PTT_SRPT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->vts_ptt_srpt) {
-    int i;
-    for(i = 0; i < ifofile->vts_ptt_srpt->nr_of_srpts; i++)
-      free(ifofile->vts_ptt_srpt->title[i].ptt);
-    free(ifofile->vts_ptt_srpt->ttu_offset);
-    free(ifofile->vts_ptt_srpt->title);
-    free(ifofile->vts_ptt_srpt);
-    ifofile->vts_ptt_srpt = 0;
-  }
-}
-
-
 int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) {
   ptl_mait_t *ptl_mait;
   int info_length;
@@ -1423,21 +1463,6 @@ int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) {
   return 1;
 }
 
-void ifoFree_PTL_MAIT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->ptl_mait) {
-    unsigned int i;
-
-    for(i = 0; i < ifofile->ptl_mait->nr_of_countries; i++) {
-      free(ifofile->ptl_mait->countries[i].pf_ptl_mai);
-    }
-    free(ifofile->ptl_mait->countries);
-    free(ifofile->ptl_mait);
-    ifofile->ptl_mait = NULL;
-  }
-}
 
 int ifoRead_VTS_TMAPT(ifo_handle_t *ifofile) {
   vts_tmapt_t *vts_tmapt;
@@ -1557,23 +1582,6 @@ int ifoRead_VTS_TMAPT(ifo_handle_t *ifofile) {
   return 1;
 }
 
-void ifoFree_VTS_TMAPT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->vts_tmapt) {
-    unsigned int i;
-
-    for(i = 0; i < ifofile->vts_tmapt->nr_of_tmaps; i++)
-      if(ifofile->vts_tmapt->tmap[i].map_ent)
-        free(ifofile->vts_tmapt->tmap[i].map_ent);
-    free(ifofile->vts_tmapt->tmap);
-    free(ifofile->vts_tmapt->tmap_offset);
-    free(ifofile->vts_tmapt);
-    ifofile->vts_tmapt = NULL;
-  }
-}
-
 
 int ifoRead_TITLE_C_ADT(ifo_handle_t *ifofile) {
 
@@ -1593,7 +1601,7 @@ int ifoRead_TITLE_C_ADT(ifo_handle_t *ifofile) {
   if(!ifoRead_C_ADT_internal(ifofile, ifofile->vts_c_adt,
                              ifofile->vtsi_mat->vts_c_adt)) {
     free(ifofile->vts_c_adt);
-    ifofile->vts_c_adt = 0;
+    ifofile->vts_c_adt = NULL;
     return 0;
   }
 
@@ -1624,7 +1632,7 @@ int ifoRead_C_ADT(ifo_handle_t *ifofile) {
 
   if(!ifoRead_C_ADT_internal(ifofile, ifofile->menu_c_adt, sector)) {
     free(ifofile->menu_c_adt);
-    ifofile->menu_c_adt = 0;
+    ifofile->menu_c_adt = NULL;
     return 0;
   }
 
@@ -1670,6 +1678,7 @@ static int ifoRead_C_ADT_internal(ifo_handle_t
*ifofile, if(info_length &&
      !(DVDReadBytes(ifofile->file, c_adt->cell_adr_table,
info_length))) { free(c_adt->cell_adr_table);
+    c_adt->cell_adr_table = NULL;
     return 0;
   }
 
@@ -1689,30 +1698,6 @@ static int ifoRead_C_ADT_internal(ifo_handle_t
*ifofile, return 1;
 }
 
-
-static void ifoFree_C_ADT_internal(c_adt_t *c_adt) {
-  if(c_adt) {
-    free(c_adt->cell_adr_table);
-    free(c_adt);
-  }
-}
-
-void ifoFree_C_ADT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  ifoFree_C_ADT_internal(ifofile->menu_c_adt);
-  ifofile->menu_c_adt = 0;
-}
-
-void ifoFree_TITLE_C_ADT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  ifoFree_C_ADT_internal(ifofile->vts_c_adt);
-  ifofile->vts_c_adt = 0;
-}
-
 int ifoRead_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) {
   if(!ifofile)
     return 0;
@@ -1730,7 +1715,7 @@ int ifoRead_TITLE_VOBU_ADMAP(ifo_handle_t
*ifofile) { if(!ifoRead_VOBU_ADMAP_internal(ifofile,
ifofile->vts_vobu_admap, ifofile->vtsi_mat->vts_vobu_admap)) {
     free(ifofile->vts_vobu_admap);
-    ifofile->vts_vobu_admap = 0;
+    ifofile->vts_vobu_admap = NULL;
     return 0;
   }
 
@@ -1761,7 +1746,7 @@ int ifoRead_VOBU_ADMAP(ifo_handle_t *ifofile) {
 
   if(!ifoRead_VOBU_ADMAP_internal(ifofile, ifofile->menu_vobu_admap,
sector)) { free(ifofile->menu_vobu_admap);
-    ifofile->menu_vobu_admap = 0;
+    ifofile->menu_vobu_admap = NULL;
     return 0;
   }
 
@@ -1796,6 +1781,7 @@ static int
ifoRead_VOBU_ADMAP_internal(ifo_handle_t
*ifofile, !(DVDReadBytes(ifofile->file, vobu_admap->vobu_start_sectors,
info_length))) { free(vobu_admap->vobu_start_sectors);
+    vobu_admap->vobu_start_sectors = NULL;
     return 0;
   }
 
@@ -1806,29 +1792,6 @@ static int
ifoRead_VOBU_ADMAP_internal(ifo_handle_t *ifofile, }
 
 
-static void ifoFree_VOBU_ADMAP_internal(vobu_admap_t *vobu_admap) {
-  if(vobu_admap) {
-    free(vobu_admap->vobu_start_sectors);
-    free(vobu_admap);
-  }
-}
-
-void ifoFree_VOBU_ADMAP(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  ifoFree_VOBU_ADMAP_internal(ifofile->menu_vobu_admap);
-  ifofile->menu_vobu_admap = 0;
-}
-
-void ifoFree_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  ifoFree_VOBU_ADMAP_internal(ifofile->vts_vobu_admap);
-  ifofile->vts_vobu_admap = 0;
-}
-
 int ifoRead_PGCIT(ifo_handle_t *ifofile) {
 
   if(!ifofile)
@@ -1848,7 +1811,7 @@ int ifoRead_PGCIT(ifo_handle_t *ifofile) {
   if(!ifoRead_PGCIT_internal(ifofile, ifofile->vts_pgcit,
                              ifofile->vtsi_mat->vts_pgcit *
DVD_BLOCK_LEN)) { free(ifofile->vts_pgcit);
-    ifofile->vts_pgcit = 0;
+    ifofile->vts_pgcit = NULL;
     return 0;
   }
 
@@ -1936,6 +1899,7 @@ static int ifoRead_PGCIT_internal(ifo_handle_t
*ifofile, pgcit_t *pgcit, ifoFree_PGC(&pgcit->pgci_srp[j].pgc);
       }
       free(pgcit->pgci_srp[i].pgc);
+      pgcit->pgci_srp[i].pgc = NULL;
       goto fail;
     }
   }
@@ -1955,7 +1919,9 @@ static void ifoFree_PGCIT_internal(pgcit_t
**pgcit) { ifoFree_PGC(&(*pgcit)->pgci_srp[i].pgc);
     }
     free((*pgcit)->pgci_srp);
+    (*pgcit)->pgci_srp = NULL;
     free(*pgcit);
+    *pgcit = NULL;
   }
   if (pgcit)
     *pgcit = NULL;
@@ -2009,13 +1975,13 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
 
   if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN)) {
     free(ifofile->pgci_ut);
-    ifofile->pgci_ut = 0;
+    ifofile->pgci_ut = NULL;
     return 0;
   }
 
   if(!(DVDReadBytes(ifofile->file, ifofile->pgci_ut, PGCI_UT_SIZE))) {
     free(ifofile->pgci_ut);
-    ifofile->pgci_ut = 0;
+    ifofile->pgci_ut = NULL;
     return 0;
   }
 
@@ -2033,13 +1999,13 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
   data = malloc(info_length);
   if(!data) {
     free(pgci_ut);
-    ifofile->pgci_ut = 0;
+    ifofile->pgci_ut = NULL;
     return 0;
   }
   if(!(DVDReadBytes(ifofile->file, data, info_length))) {
     free(data);
     free(pgci_ut);
-    ifofile->pgci_ut = 0;
+    ifofile->pgci_ut = NULL;
     return 0;
   }
 
@@ -2047,7 +2013,7 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
   if(!pgci_ut->lu) {
     free(data);
     free(pgci_ut);
-    ifofile->pgci_ut = 0;
+    ifofile->pgci_ut = NULL;
     return 0;
   }
   ptr = data;
@@ -2086,8 +2052,9 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
         ifoFree_PGCIT_internal(&pgci_ut->lu[j].pgcit);
       }
       free(pgci_ut->lu);
+      pgci_ut->lu = NULL;
       free(pgci_ut);
-      ifofile->pgci_ut = 0;
+      ifofile->pgci_ut = NULL;
       return 0;
     }
     pgci_ut->lu[i].pgcit->ref_count = 1;
@@ -2099,8 +2066,9 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
         ifoFree_PGCIT_internal(&pgci_ut->lu[j].pgcit);
       }
       free(pgci_ut->lu);
+      pgci_ut->lu = NULL;
       free(pgci_ut);
-      ifofile->pgci_ut = 0;
+      ifofile->pgci_ut = NULL;
       return 0;
     }
     /* FIXME: Iterate and verify that all menus that should exists
accordingly @@ -2111,22 +2079,6 @@ int ifoRead_PGCI_UT(ifo_handle_t
*ifofile) { }
 
 
-void ifoFree_PGCI_UT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->pgci_ut) {
-    unsigned int i;
-
-    for(i = 0; i < ifofile->pgci_ut->nr_of_lus; i++) {
-      ifoFree_PGCIT_internal(&ifofile->pgci_ut->lu[i].pgcit);
-    }
-    free(ifofile->pgci_ut->lu);
-    free(ifofile->pgci_ut);
-    ifofile->pgci_ut = 0;
-  }
-}
-
 static int ifoRead_VTS_ATTRIBUTES(ifo_handle_t *ifofile,
                                   vts_attributes_t *vts_attributes,
                                   unsigned int offset) {
@@ -2206,7 +2158,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
 
   if(!(DVDReadBytes(ifofile->file, vts_atrt, VTS_ATRT_SIZE))) {
     free(vts_atrt);
-    ifofile->vts_atrt = 0;
+    ifofile->vts_atrt = NULL;
     return 0;
   }
 
@@ -2223,7 +2175,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
   data = malloc(info_length);
   if(!data) {
     free(vts_atrt);
-    ifofile->vts_atrt = 0;
+    ifofile->vts_atrt = NULL;
     return 0;
   }
 
@@ -2232,7 +2184,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
   if(!(DVDReadBytes(ifofile->file, data, info_length))) {
     free(data);
     free(vts_atrt);
-    ifofile->vts_atrt = 0;
+    ifofile->vts_atrt = NULL;
     return 0;
   }
 
@@ -2246,7 +2198,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
   if(!vts_atrt->vts) {
     free(data);
     free(vts_atrt);
-    ifofile->vts_atrt = 0;
+    ifofile->vts_atrt = NULL;
     return 0;
   }
   for(i = 0; i < vts_atrt->nr_of_vtss; i++) {
@@ -2255,7 +2207,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
                                (sector * DVD_BLOCK_LEN) + offset)) {
       free(data);
       free(vts_atrt);
-      ifofile->vts_atrt = 0;
+      ifofile->vts_atrt = NULL;
       return 0;
     }
 
@@ -2268,19 +2220,6 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
 }
 
 
-void ifoFree_VTS_ATRT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->vts_atrt) {
-    free(ifofile->vts_atrt->vts);
-    free(ifofile->vts_atrt->vts_atrt_offsets);
-    free(ifofile->vts_atrt);
-    ifofile->vts_atrt = 0;
-  }
-}
-
-
 int ifoRead_TXTDT_MGI(ifo_handle_t *ifofile) {
   txtdt_mgi_t *txtdt_mgi;
 
@@ -2307,7 +2246,7 @@ int ifoRead_TXTDT_MGI(ifo_handle_t *ifofile) {
   if(!(DVDReadBytes(ifofile->file, txtdt_mgi, TXTDT_MGI_SIZE))) {
     fprintf(stderr, "libdvdread: Unable to read TXTDT_MGI.\n");
     free(txtdt_mgi);
-    ifofile->txtdt_mgi = 0;
+    ifofile->txtdt_mgi = NULL;
     return 0;
   }
 
@@ -2315,12 +2254,18 @@ int ifoRead_TXTDT_MGI(ifo_handle_t *ifofile) {
   return 1;
 }
 
-void ifoFree_TXTDT_MGI(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
+void ifoFree_VTS_TMAPT(ifo_handle_t *ifofile) {
 
-  if(ifofile->txtdt_mgi) {
-    free(ifofile->txtdt_mgi);
-    ifofile->txtdt_mgi = 0;
+  if(ifofile->vts_tmapt) {
+    unsigned int i;
+
+    for(i = 0; i < ifofile->vts_tmapt->nr_of_tmaps; i++)
+      if(ifofile->vts_tmapt->tmap[i].map_ent) {
+        free(ifofile->vts_tmapt->tmap[i].map_ent);
+      }
+    free(ifofile->vts_tmapt->tmap);
+    free(ifofile->vts_tmapt->tmap_offset);
+    free(ifofile->vts_tmapt);
   }
+
 }


More information about the DVDnav-discuss mailing list