diff --git a/src/cats/grids/cats_grid.c b/src/cats/grids/cats_grid.c index 41ce132589699fe2ec8e8fe8d850673460c2cab2..61ba30239c7e478f7371e571a03c7affec6dd205 100644 --- a/src/cats/grids/cats_grid.c +++ b/src/cats/grids/cats_grid.c @@ -251,30 +251,33 @@ void cleanup_grid_seeds_and_juveniles(struct cats_grid *grid) free_grid(&grid->seeds_produced, grid->dimension.rows); free_grid(&grid->dispersed_seeds, grid->dimension.rows); + if (grid->seed_bank) { for (cats_dt_coord row = 0; row < grid->dimension.rows; row++) { for (cats_dt_coord col = 0; col < grid->dimension.cols; col++) { destroy_seed_structure(grid, row, col); } - free(grid->juveniles[row]); + free(grid->seed_bank[row]); } } - if (grid->seed_bank) { + if (grid->juveniles) { for (cats_dt_coord row = 0; row < grid->dimension.rows; row++) { for (cats_dt_coord col = 0; col < grid->dimension.cols; col++) { destroy_juveniles(grid, row, col); } free(grid->juveniles[row]); + grid->juveniles[row] = NULL; } } free(grid->seed_bank); free(grid->juveniles); - free(grid->seeds_produced); + grid->juveniles = NULL; + grid->seed_bank = NULL; } diff --git a/src/cats/plants/plant_structures.c b/src/cats/plants/plant_structures.c index be4613afb7be7f1cc6d76aa7f376c241dd2b8f10..d75d8f482c6ce09d617fcd346c464115bb2f1865 100644 --- a/src/cats/plants/plant_structures.c +++ b/src/cats/plants/plant_structures.c @@ -114,6 +114,7 @@ void destroy_juveniles(const struct cats_grid *grid, const cats_dt_coord row, co assert(stages >= 0); if (grid->juveniles[row] && grid->juveniles[row][col]) { + memset(grid->juveniles[row][col], 0, stages * sizeof(cats_dt_population)); free(grid->juveniles[row][col]); grid->juveniles[row][col] = NULL; }