@@ -897,6 +897,11 @@ cdef class BlockValuesRefs:
897897 else:
898898 self.referenced_blocks = []
899899
900+ def _clear_dead_references(self ) -> None:
901+ self.referenced_blocks = [
902+ ref for ref in self .referenced_blocks if ref() is not None
903+ ]
904+
900905 def add_reference(self , blk: Block ) -> None:
901906 """Adds a new reference to our reference collection.
902907
@@ -905,6 +910,7 @@ cdef class BlockValuesRefs:
905910 blk : Block
906911 The block that the new references should point to.
907912 """
913+ self._clear_dead_references()
908914 self.referenced_blocks.append(weakref.ref(blk ))
909915
910916 def add_index_reference(self , index: object ) -> None:
@@ -915,6 +921,7 @@ cdef class BlockValuesRefs:
915921 index : Index
916922 The index that the new reference should point to.
917923 """
924+ self._clear_dead_references()
918925 self.referenced_blocks.append(weakref.ref(index ))
919926
920927 def has_reference(self ) -> bool:
@@ -927,8 +934,6 @@ cdef class BlockValuesRefs:
927934 -------
928935 bool
929936 """
930- self.referenced_blocks = [
931- ref for ref in self .referenced_blocks if ref() is not None
932- ]
937+ self._clear_dead_references()
933938 # Checking for more references than block pointing to itself
934939 return len(self.referenced_blocks ) > 1
0 commit comments