From 641e06e67c4358283eb35de7ffa269246e01d1a2 Mon Sep 17 00:00:00 2001
From: Seth Hillbrand <seth@kipro-pcb.com>
Date: Fri, 7 Mar 2025 17:29:57 -0800
Subject: [PATCH] Removed shared_ptr circular references

When A references B and B references A, the shared pointer reference
count will never go to zero by just removing the parent container.  We
need to explicitly clear the shared pointer references when we are done

Fixes https://gitlab.com/kicad/code/kicad/-/issues/20272
---
 pcbnew/drc/drc_creepage_utils.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/pcbnew/drc/drc_creepage_utils.h b/pcbnew/drc/drc_creepage_utils.h
index 0c977b3322..70be2c6071 100644
--- a/pcbnew/drc/drc_creepage_utils.h
+++ b/pcbnew/drc/drc_creepage_utils.h
@@ -697,11 +697,29 @@ public:
     ~CREEPAGE_GRAPH()
     {
         for( CREEP_SHAPE* cs : m_shapeCollection )
+        {
             if( cs )
             {
                 delete cs;
                 cs = nullptr;
             }
+        }
+
+        // Clear out the circular shared pointer references
+        for( std::shared_ptr<GRAPH_NODE>& n : m_nodes )
+        {
+            if( n )
+            {
+                n->m_node_conns.clear();
+                n = nullptr;
+            }
+        }
+
+        for( std::shared_ptr<GRAPH_CONNECTION>& c : m_connections )
+        {
+            if( c )
+                c = nullptr;
+        }
     };