GridNode array possible memory leak?

Hi!

I’m using pro version of the “A* Pathfinding Project”, version 3.5.1 to be precise.

I use a grid graph and build it on the fly using data from the tile system. Once in the location I create the nodes array and attach it to the graph roughly as follows:



if(g != null)
  astar.astarData.RemoveGraph(g);

g = astar.astarData.CreateGraph(typeof(Pathfinding.GridGraph)) as Pathfinding.GridGraph;
astar.astarData.AddGraph(g as Pathfinding.NavGraph);

g.width = ts.columns;
g.depth = ts.rows;
g.size = new Vector2(ts.tileSize.x, ts.tileSize.z);

g.UpdateSizeFromWidthDepth();

g.GenerateMatrix();
g.SetUpOffsetsAndCosts();

int graphIndex = AstarPath.active.astarData.GetGraphIndex(g);
GridNode.SetGridGraph(graphIndex, g);

GridNode[] nodes = new GridNode[width*depth];
for(int i=0;i<nodes.Length;i++)
{
  nodes[i] = new GridNode(g.active);
  nodes[i].GraphIndex = (uint)graphIndex;
}

g.nodes = nodes;

for(int z = 0; z < g.depth; z++)
{
  for(int x = 0; x < g.width; x++)
  {
    if(IsTileWalkable(z,x))
    {
      int idx = z*g.width+x;
      GridNode node = g.nodes[idx];
      node.NodeInGridIndex = idx;
      node.Penalty = 1;
      node.Walkable = true;
      node.WalkableErosion = true;
    }
  }
}

for(int z = 0; z < g.depth; z++)
{
  for(int x = 0; x < g.width; x++)
  {
    if(IsTileWalkable(z,x))
    {
      int idx = z*g.width+x;
      GridNode node = g.nodes[idx];
      g.CalculateConnections(g.nodes,x,z,node);
    }
  }
}

g.ErodeWalkableArea();


It all works just fine, however according to Unity’s internal profiler Mono memory occupied by previous nodes array is not reclaimed. I don’t store any references to nodes array and as you can see I do destroy the previous graph before adding the new one.

I have locations which can be in size up to 200x300 tiles where each allocation of nodes can take up to 5-7 Mb and since memory is not reclaimed it’s going to be exhausted quite soon.

In order to fix this issue I’m caching grid nodes and re-use the nodes array from the cache if the cached array fits the location total tiles amount (columns*rows). It works just fine, however I had an urge to report this issue :wink: