Index: /trunk/Mars/mcore/zofits.h
===================================================================
--- /trunk/Mars/mcore/zofits.h	(revision 17287)
+++ /trunk/Mars/mcore/zofits.h	(revision 17288)
@@ -182,6 +182,13 @@
             SetInt(  "ZSHRINK",  1,               "Catalog shrink factor");
 
-            fCatalogSize      = 0;
+            fCatalogSize   = 0;
+            fRealRowWidth  = 0;
+            fCatalogOffset = 0;
+            fCatalogSize   = 0;
+            fCheckOffset   = 0;
+
+            fRealColumns.clear();
             fCatalog.clear();
+            fCatalogSum.reset();
             fRawSum.reset();
         }
@@ -286,4 +293,5 @@
                 std::rethrow_exception(fThreadsException);
 #endif
+
             //copy current row to pool or rows waiting for compression
             char* target_location = fSmartBuffer.get() + fRealRowWidth*(fTable.num_rows%fNumRowsPerTile);
@@ -362,5 +370,5 @@
 
         /// Shrinks a catalog that is too long to fit into the reserved space at the beginning of the file.
-        void ShrinkCatalog()
+        const uint32_t ShrinkCatalog()
         {
             //add empty row to get either the target number of rows, or a multiple of the allowed size
@@ -370,5 +378,5 @@
             //did we write more rows than what the catalog could host ?
             if (fCatalogSize <= fNumTiles) // nothing to do
-                return;
+                return 1;
 
             //always exact as extra rows were added just above
@@ -409,4 +417,6 @@
             SetInt("ZTILELEN", fNumRowsPerTile);
             SetInt("ZSHRINK",  shrink_factor);
+
+            return shrink_factor;
         }
 
@@ -457,5 +467,5 @@
             }
 
-            ShrinkCatalog();
+            const uint32_t shrink_factor = ShrinkCatalog();
 
             //update header keywords
@@ -471,5 +481,5 @@
             SetInt("NAXIS1", total_catalog_width);
             SetInt("NAXIS2", total_num_tiles_written);
-            SetStr("RAWSUM", std::to_string(fRawSum.val()));
+            SetStr("RAWSUM", std::to_string((long long int)(fRawSum.val())));
 
             const float compression_ratio = (float)(fRealRowWidth*fTable.num_rows)/(float)heap_size;
@@ -489,4 +499,10 @@
 
             std::ofstream::close();
+
+            fSmartBuffer = std::shared_ptr<char>();
+
+            //restore the number of rows per tile in case the catalog has been shrinked
+            if (shrink_factor != 1)
+                fNumRowsPerTile /= shrink_factor;
 
             if ((checksm+fDataSum).valid())
@@ -530,6 +546,6 @@
             fRealColumns.emplace_back(col, comp);
 
-            SetStr("ZFORM"+std::to_string(fRealColumns.size()), std::to_string(cnt)+typechar, "format of "+name+" "+CommentFromType(typechar));
-            SetStr("ZCTYP"+std::to_string(fRealColumns.size()), "FACT", "Compression type: FACT");
+            SetStr("ZFORM"+std::to_string((long long int)(fRealColumns.size())), std::to_string((long long int)(cnt))+typechar, "format of "+name+" "+CommentFromType(typechar));
+            SetStr("ZCTYP"+std::to_string((long long int)(fRealColumns.size())), "FACT", "Compression type: FACT");
 
             return true;
