Skip to content

Commit d2e913a

Browse files
authored
Cleanup node on Crystal::PointerLinkedList#delete (#16398)
Calls to delete, pop or shift a node from the list now clear the `#next` and `#previous` properties of the node. It's not required per-se, but cleaning up pointers is never a bad idea.
1 parent 2ffc672 commit d2e913a

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

spec/std/crystal/pointer_linked_list_spec.cr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ describe Crystal::PointerLinkedList do
8787
list.push pointerof(w)
8888

8989
list.delete pointerof(y)
90+
y.next.should eq(Pointer(TestedObject).null)
91+
y.previous.should eq(Pointer(TestedObject).null)
9092

9193
ExpectOrderHelper.by_next(x, z, w, x)
9294
ExpectOrderHelper.by_previous(x, w, z, x)
@@ -113,6 +115,8 @@ describe Crystal::PointerLinkedList do
113115

114116
obj.should_not be_nil
115117
obj.should eq(pointerof(x))
118+
obj.not_nil!.value.next.should eq(Pointer(TestedObject).null)
119+
obj.not_nil!.value.previous.should eq(Pointer(TestedObject).null)
116120

117121
ExpectOrderHelper.by_next(y, z, w, y)
118122
ExpectOrderHelper.by_previous(y, w, z, y)
@@ -147,6 +151,8 @@ describe Crystal::PointerLinkedList do
147151

148152
obj.should_not be_nil
149153
obj.should eq(pointerof(w))
154+
obj.not_nil!.value.next.should eq(Pointer(TestedObject).null)
155+
obj.not_nil!.value.previous.should eq(Pointer(TestedObject).null)
150156

151157
ExpectOrderHelper.by_next(x, y, z, x)
152158
ExpectOrderHelper.by_previous(x, z, y, x)

src/crystal/pointer_linked_list.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ struct Crystal::PointerLinkedList(T)
6363
else
6464
@head = Pointer(T).null
6565
end
66+
67+
node.value.next = Pointer(T).null
68+
node.value.previous = Pointer(T).null
6669
end
6770

6871
# Removes and returns head from the list, yields if empty

0 commit comments

Comments
 (0)