From 73f4a2ee0984cb7f2395fb27925301c77755b33d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 29 Mar 2026 17:44:32 +0000 Subject: [PATCH] Fix memory leak and segfault risk in initializeMap This commit fixes a memory leak when `initializeMap` is called with a new map, as the existing `gridMap` was not being released. It also resolves a potentially fatal segmentation fault/invalid access bug in `initializeEmpty`, where old data arrays were incorrectly deallocated using the new map dimensions (if the map changed size) before they had actually been released. Fixes based on PR #62 but implemented robustly. Co-authored-by: karlkurzer <10877966+karlkurzer@users.noreply.github.com> --- src/dynamicvoronoi.cpp | 20 +++++++++++++++----- tests/test_voronoi.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 tests/test_voronoi.cpp diff --git a/src/dynamicvoronoi.cpp b/src/dynamicvoronoi.cpp index 198399da..0cd8812d 100644 --- a/src/dynamicvoronoi.cpp +++ b/src/dynamicvoronoi.cpp @@ -23,20 +23,24 @@ DynamicVoronoi::~DynamicVoronoi() { } void DynamicVoronoi::initializeEmpty(int _sizeX, int _sizeY, bool initGridMap) { - sizeX = _sizeX; - sizeY = _sizeY; if (data) { for (int x=0; x + +using namespace HybridAStar; + +int main() { + DynamicVoronoi voronoi; + + // Create a 10x10 map + int width = 10; + int height = 10; + bool** map1 = new bool*[width]; + for (int i = 0; i < width; ++i) { + map1[i] = new bool[height](); + } + + // Initialize map + std::cout << "Initializing 10x10 map..." << std::endl; + voronoi.initializeMap(width, height, map1); + + // Create a 20x20 map + int new_width = 20; + int new_height = 20; + bool** map2 = new bool*[new_width]; + for (int i = 0; i < new_width; ++i) { + map2[i] = new bool[new_height](); + } + + // Re-initialize map (which should free previous map resources or cause bug with current code) + std::cout << "Re-initializing with 20x20 map..." << std::endl; + voronoi.initializeMap(new_width, new_height, map2); + + std::cout << "Done!" << std::endl; + return 0; +}