Search code examples
c#recursionstack-overflowcallstack

Solve Infinite Recursion in c#


I know I'm having an infinite recursion problem in my code with a stack overflow. I just don't know how to go about fixing it, help would be appreciated.

public Point WorldToMapCell(Point worldPoint)
{
    return WorldToMapCell(new Point((int)worldPoint.X, (int)worldPoint.Y));
}

public MapCell GetCellAtWorldPoint(Point worldPoint)
{
    Point mapPoint = WorldToMapCell(worldPoint);
    return Rows[mapPoint.Y].Columns[mapPoint.X];
}

public MapCell GetCellAtWorldPoint(Vector2 worldPoint)
{
    return GetCellAtWorldPoint(new Point((int)worldPoint.X, (int)worldPoint.Y));
}

Solution

  • Infinite recursion (and the resulting stack overflow) happens when you've got a function that directly, or indirectly, calls itself repeatedly without any opportunity for it to stop doing that. Your first function, WorldToMapCell calls itself unconditionally, causing this problem.