Hi all,
Context:
I am using Swift for the 2020 Advent of Code, specifically Day 23 (Day 23 - Advent of Code 2020). So, if you're participating and haven't finished this yet, there are spoilers.
Explanation:
I have a linked list -esque Node structure, with a value and a next pointer.
class Node<T> {
public let value: T
public var next: Node<T>?
init(_ val: T, _ n: Node<T>?) {
value = val
next = n
}
}
In one function call I do two things:
- Construct a Map of the
value
to theNode<Int>
with 1 million values - Construct a linked list of those 1 million values
I finally return two values from the linked list in the function as an [Int]
. However, there is a short delay (when debugging in debug mode in Xcode), before I get an EXC_BAD_ACCESS
.
The following are screenshots of the exception in action:
It looks like it's coming from the Node<Int>
implicit deinit function.
The solution to this for me was to manually set the .next
to nil
for all of the values (lines 488-490 in the first screenshot). When doing this, the return is instantaneous and has no exception.
I'm on Xcode 12.3 on Big Sur, with Swift 5.3.2.
Conclusion:
First, I find it hilarious that I (potentially) found a bug in Swift through an Advent of Code problem. If I'm right, then I hope you all can have a good laugh too!
Second, what does everyone think? With the only change being to uncomment those lines to remove the problem, it seems like this might be a problem. Unless I'm missing something fundamental about how to use Swift. I'm guessing that ARC doesn't like 1-million-deep linked lists, perhaps?
If this is a bug, should I post it anywhere besides this forum?
I'm sure there's a way to reproduce it without all of my code. It will take ~30s to run in debug mode or ~9s in release mode to get to the point where the error occurs. As compiler bugs can be tricky, I didn't want to change anything and lose the error.
Links:
-
https://github.com/Jman012/AdventOfCode2020/blob/swiftbugaocday23/Sources/AdventOfCode2020/Day23_01.swift#L142
- I've Linked to the specific tag for this file instead of the latest where I might change it further. I've also pointed to line 142 where the
.next=nil
operation occurs (after removing other code from above it in the screenshots).
- I've Linked to the specific tag for this file instead of the latest where I might change it further. I've also pointed to line 142 where the