Ref count the gnumeric reader
authorJohn Darrington <john@darrington.wattle.id.au>
Fri, 8 Mar 2013 15:10:14 +0000 (16:10 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Sat, 9 Mar 2013 14:52:15 +0000 (15:52 +0100)
src/data/gnumeric-reader.c

index 7dbc63a9d681fbea37e205ab761917d9904765de..71dc4a1be7de28d2abbca4d52b13dfbeb13dfd4b 100644 (file)
@@ -104,6 +104,7 @@ struct sheet_detail
 struct gnumeric_reader
 {
   struct spreadsheet spreadsheet;
+  int ref_cnt;
 
   /* The libxml reader for this instance */
   xmlTextReaderPtr xtr;
@@ -138,16 +139,20 @@ void
 gnumeric_destroy (struct spreadsheet *s)
 {
   struct gnumeric_reader *r = s;
-  int i;
 
-  for (i = 0; i < s->n_sheets; ++i)
+  if (0 == --r->ref_cnt)
     {
-      xmlFree (r->sheets[i].name);
-    }
+      int i;
+
+      for (i = 0; i < s->n_sheets; ++i)
+       {
+         xmlFree (r->sheets[i].name);
+       }
     
-  free (r->sheets);
+      free (r->sheets);
 
-  free (r);
+      free (r);
+    }
 }
 
 
@@ -496,6 +501,7 @@ gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_erro
   r->row = r->col = -1;
   r->state = STATE_PRE_INIT;
   r->xtr = xtr;
+  r->ref_cnt++;
 
   /* Advance to the start of the workbook.
      This gives us some confidence that we are actually dealing with a gnumeric
@@ -565,6 +571,8 @@ gnumeric_make_reader (struct spreadsheet *spreadsheet,
   if (r->row != -1)
     r = gnumeric_reopen (r, NULL, true);
 
+
+
   if ( opts->cell_range )
     {
       if ( ! convert_cell_ref (opts->cell_range,