Skip to content

Potential memory leak in SimpleHeap's removed_node_tuples #71

@codelion

Description

@codelion

Hey there, I've been investigating a memory issue in the SimpleHeap implementation and found that removed_node_tuples can grow indefinitely during pathfinding, especially on larger grids.

The problem is that when nodes are "removed" via remove_node(), they get added to removed_node_tuples but are never cleaned up. Over time, this set just keeps growing.

After digging into the code, I'm wondering if removed_node_tuples is actually necessary at all? From what I can see, the A* algorithm already marks processed nodes with the closed flag, so we could potentially simplify things by:

  1. Getting rid of removed_node_tuples completely
  2. Having pop_node() just skip any nodes that are already closed
  3. Making remove_node() do nothing (or removing it entirely)

Something like:

def pop_node(self):
    while self.open_list:
        node_tuple = heapq.heappop(self.open_list)
        # ... get node from tuple ...
        if not node.closed:
            return node
    return None

I tested this approach and it seems to work correctly while eliminating the memory leak. The pathfinding results are identical, but without the growing memory usage.

Is there a specific reason removed_node_tuples was implemented this way, or would you be interested in a PR that simplifies this? I might be missing some edge case that requires the current approach, so wanted to check first.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions