From 128b81af43248a45f53e2cc8dd6d16262ea5663b Mon Sep 17 00:00:00 2001
From: Andreas Gattringer <andreas.gattringer@univie.ac.at>
Date: Thu, 11 May 2023 09:47:20 +0200
Subject: [PATCH] memory handling fix with custom stats

---
 src/cats/stats/grid_stats.c | 6 ++++--
 src/cats/stats/statistics.c | 7 +++++--
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/cats/stats/grid_stats.c b/src/cats/stats/grid_stats.c
index 49b48a8..d018b5e 100644
--- a/src/cats/stats/grid_stats.c
+++ b/src/cats/stats/grid_stats.c
@@ -144,8 +144,10 @@ void consolidate_thread_stats(struct cats_grid *grid, struct cats_configuration
                                 g->stats.stats[stat_idx] += threads[i].stats[class].stats[stat_idx];
                         }
 
-                        for (int64_t j = 0; j < threads[i].stats->custom_stat_count; j++){
-                                g->stats.custom_stats[j] += threads[i].stats[class].custom_stats[j];
+                        if (g->stats.custom_stats) {
+                                for (int64_t j = 0; j < threads[i].stats->custom_stat_count; j++){
+                                        g->stats.custom_stats[j] += threads[i].stats[class].custom_stats[j];
+                                }
                         }
                 }
         }
diff --git a/src/cats/stats/statistics.c b/src/cats/stats/statistics.c
index 73e4c20..d853989 100644
--- a/src/cats/stats/statistics.c
+++ b/src/cats/stats/statistics.c
@@ -59,8 +59,10 @@ void zero_statistics_stats(struct statistics *stats)
                 stats->stats[i] = 0;
         }
 
-        for (int64_t i = 0; i < stats->custom_stat_count; i++) {
-                stats->custom_stats[i] = 0;
+        if (stats->custom_stats != NULL) {
+                for (int64_t i = 0; i < stats->custom_stat_count; i++) {
+                        stats->custom_stats[i] = 0;
+                }
         }
 
 }
@@ -83,6 +85,7 @@ void cleanup_statistics(struct statistics *stats)
 {
         if (stats->stats_header) free_string_array(&stats->stats_header);
         free(stats->custom_stats);
+        stats->custom_stats = NULL;
 }
 
 
-- 
GitLab