Stack Overflow

I’ve started getting stack overflow errors with A* Pathfinding 3.1.4. I recently upgraded from 3.0.9 and I wasn’t getting the error in that version, and the problem seems to be happening in code that I haven’t changed since the upgrade. I’m using Unity 4.

Here’s the error message, it recurses in this function about 1000 times:
StackOverflowException: The requested operation caused a stack overflow.
at Pathfinding.Nodes.GridNode.UpdateAllG (Pathfinding.NodeRun nodeR, Pathfinding.NodeRunData nodeRunData) [0x0007e] in C:\Projects\TowerDefense\Assets\AstarPathfindingProject\Generators\odeClasses\GridNode.cs:205

(Filename: Assets/AstarPathfindingProject/Generators/NodeClasses/GridNode.cs Line: 205)

I’ve got one grid graph with 2542 nodes, and I’m getting this when I UpdateGraphs with a GraphUpdateObject. Here’s my code.
void SetPathfindingWalkable(bool walkable)
GraphUpdateObject guo = new GraphUpdateObject (pathfindingCollider.bounds);

	guo.addPenalty = -PathfindingPenalty;
	guo.addPenalty = PathfindingPenalty;

guo.updatePhysics = false;
if( (guo);


I’m making a tower defense game where I’m constantly adding and removing towers, and this function is called every time a building is added or removed. I get the error after a random number of updates. I’ve tried with automatic multithreading, one, two, and none, and I still get the error. Any idea of how this could be fixed?


Make sure that update is not causing negative penalties. Negative penalties could really screw up pathfinding and in the worst case make it enter an infinite loop. In 3.2 I have a warning if that happens, but in 3.1.4 there is no such warning.

Try for a moment to comment out the line which subtracts penalty from nodes and see if it happens then.

Now I’ve made sure that I remove the exact penalty that I add when placing the tower, and I haven’t gotten the error yet, that seems to have fixed it.