fix for bug #244, "Segfault if file cannot be found and --stats is on"
authorMorgan Deters <mdeters@gmail.com>
Thu, 3 Mar 2011 18:35:17 +0000 (18:35 +0000)
committerMorgan Deters <mdeters@gmail.com>
Thu, 3 Mar 2011 18:35:17 +0000 (18:35 +0000)
src/main/main.cpp
src/util/stats.h

index fcd322e993362ed4f903564b0f329f805408dfc0..563fa472e10c8abdc058e528b61c6c8f5c994c54 100644 (file)
@@ -181,7 +181,7 @@ int runCvc4(int argc, char* argv[]) {
   const char* filename = inputFromStdin ? "<stdin>" : argv[firstArgIndex];
 
   ReferenceStat< const char* > s_statFilename("filename", filename);
-  StatisticsRegistry::registerStat(&s_statFilename);
+  RegisterStatistic statFilenameReg(&s_statFilename);
 
   if(options.inputLanguage == language::input::LANG_AUTO) {
     if( inputFromStdin ) {
@@ -273,15 +273,12 @@ int runCvc4(int argc, char* argv[]) {
 #endif
 
   ReferenceStat< Result > s_statSatResult("sat/unsat", result);
-  StatisticsRegistry::registerStat(&s_statSatResult);
+  RegisterStatistic statSatResultReg(&s_statSatResult);
 
   if(options.statistics) {
     StatisticsRegistry::flushStatistics(*options.err);
   }
 
-  StatisticsRegistry::unregisterStat(&s_statSatResult);
-  StatisticsRegistry::unregisterStat(&s_statFilename);
-
   return returnValue;
 }
 
index bf2e9d46ccd12af1a9528c6e9302cd76dc7f3630..978893a51552aae9a7a2d6e07cc22bb9998c9a5a 100644 (file)
@@ -703,6 +703,26 @@ public:
     }                                                                   \
   } _StatField
 
+/**
+ * Resource-acquisition-is-initialization idiom for statistics
+ * registry.  Useful for stack-based statistics (like in the driver).
+ * Generally, for statistics kept in a member field of class, it's
+ * better to use the above KEEP_STATISTIC(), which does declaration of
+ * the member, construction of the statistic, and
+ * registration/unregistration.  This RAII class only does
+ * registration and unregistration.
+ */
+class RegisterStatistic {
+  Stat* d_stat;
+public:
+  RegisterStatistic(Stat* stat) : d_stat(stat) {
+    StatisticsRegistry::registerStat(d_stat);
+  }
+  ~RegisterStatistic() {
+    StatisticsRegistry::unregisterStat(d_stat);
+  }
+};/* class RegisterStatistic */
+
 #undef __CVC4_USE_STATISTICS
 
 }/* CVC4 namespace */