title: Thread 1: EXC_BAD_ACCESS (code=2, address=0x16d0f3ff0)
body: <p>I am developing an app (in <code>XCode</code> Version 11.2 and Swift 4.2) in which I fill in a LinkedList and after working with it, removing the elements that compose it produces the <code>Thread 1 error: EXC_BAD_ACCESS (code = 2, address = 0x16d0f3ff0)</code>. The error occurs even without working with the items in the list, simply by adding them and trying to eliminate them the error already occurs. The tests I am doing with an iPhone with <code>IOS version 11.4.1</code></p>
  
  <p>The implementation of the LinkedList is as follows:</p>
  
  <pre><code>import Foundation
  
  public class Node&lt;T&gt; {
  
      var value: T
      var next: Node&lt;T&gt;?
      weak var previous: Node&lt;T&gt;?
  
      init(value: T) {
          self.value = value
      } // init
  } // Node
  
  public class LinkedList&lt;T&gt; {
  
      private var head: Node&lt;T&gt;?
  
      private var tail: Node&lt;T&gt;?
  
      public private(set) var count: Int = 0
  
      public init() { } // init
  
      public var isEmpty: Bool {
          return  self.head == nil
      } // isEmpty
  
      public var first: Node&lt;T&gt;? {
          return  self.head
      } // first
  
      public var last: Node&lt;T&gt;? {
          return  self.tail
      } // last
  
      public func nodeAt(index: Int) -&gt; Node&lt;T&gt;? {
          if index &gt;= 0 {
              var node =  self.head
              var i = index
              while node != nil {
                  if i == 0 {
                      return node
                  } // if
  
                  i -= 1
                  node = node!.next
              } // while
          } // if
  
          return nil
      } // nodeAt
  
      public func removeAll() {
          self.head = nil
          self.tail = nil
  
          self.count = 0
      } // removeAll
  
      public func remove(node: Node&lt;T&gt;?) -&gt; String {
          if isEmpty {
              return String("ERROR: Empty list, nothing to remove.")
          } // if
  
          guard node != nil else {
             return String("ERROR: Invalid node, nothing to remove.")
          } // guard
  
          let prev = node?.previous
          let next = node?.next
  
          if next != nil &amp;&amp; prev == nil {
              self.head = next
              next?.previous = nil
          } else if next != nil &amp;&amp; prev != nil {
              prev?.next = next
              next?.previous = prev
          } else if next == nil &amp;&amp; prev != nil {
               self.tail = prev
              prev?.next = nil
          } // if
          node?.previous = nil
          node?.next = nil
  
          self.count -= 1
          return String("Successfully removed node: \(node!.value)")
      } // remove
  
      func enqueue(value: T) {
          let newNode = Node(value: value)
  
          if let tailNode =  self.tail {
                 newNode.previous = tailNode
                 tailNode.next = newNode
          } else {
                  self.head = newNode
          } // else
          self.tail = newNode
          self.count += 1
      }
  
      func enqueue_first(value: HexRecord) {
          let newNode = Node(value: value)
  
          if let headNode = self.head {
              newNode.next = headNode
              headNode.previous = newNode
          } 
          self.head = newNode
          self.count += 1
      }
  
      func dequeue() -&gt; T? {
          let element =  self.head?.value
          self.head =  self.head?.next
          self.count -= 1
  
          return element
      }
  
  } // LinkedList
  
  </code></pre>
  
  <p>The nodes of the same are of the HexRecord type:</p>
  
  <pre><code>public class HexRecord
  {
      private var length: Int = 0
      private var address: Int64 = 0
      private var type: Int32 = 0
      private var data = [UInt8] ()
      private var checksum: UInt8 = 0
  
      init()
      {
  
      }
  
      public func getAddress() -&gt; Int64 {
          return address;
      }
  
      public func getType() -&gt; Int32 {
          return type;
      }
  
      public func getData() -&gt; [UInt8] {
          return data;
      }
  
      public func getLength() -&gt; Int {
          return length;
      }
  
      public func getChecksum() -&gt; UInt8 {
          return checksum;
      }
  
  
      public func setAddress(address: Int64) {
          self.address = address;
      }
  
      public func setData(data: [UInt8]) {
          self.data = data;
      }
  
      public func setLength(length: Int) {
          self.length = length;
      }
  
      public func setType(type: Int32) {
          self.type = type;
      }
  
      public func setChecksum(checksum: UInt8) {
          self.checksum = checksum;
      }
  }
  
  </code></pre>
  
  <p>It is used as follows:</p>
  
  <pre><code>func tratar_registros() {
  
      var records = LinkedList&lt;HexRecord&gt;();
  
      ....
  
      let data_line: HexRecord? = try parseRecord(line: line)  // parseRecord convert String to HexRecord
      if (data_line != nil)
      {
          records.enqueue(value: data_line!)
      }
      ....
  
      records.removeAll();        //Thread 1: EXC_BAD_ACCESS (code=2, address=0x16d0f3ff0)
  
  } // Thread 1: EXC_BAD_ACCESS (code=2, address=0x16d0f3ff0)     if there is no line records.removeAll();
  
  </code></pre>
  
  <p>With the debugger I see that the error occurs when assigning the value nil to self.head. Just before self.head has correct values, it becomes nil and the error is skipped before reaching the next instruction (within the removeAll function)</p>
  
  <p>In the Debug Navigator, in the stackTrace the last 2 functions where the error is seen:</p>
  
  <pre><code>libobjc.A.dylib`_object_remove_assocations:
      0x180d11eec &lt;+0&gt;:   sub    sp, sp, #0x70             ; =0x70 
  -&gt;  0x180d11ef0 &lt;+4&gt;:   stp    x26, x25, [sp, #0x20]  //Thread 1 error: EXC_BAD_ACCESS (code = 2, address = 0x16d0f3ff0)
      0x180d11ef4 &lt;+8&gt;:   stp    x24, x23, [sp, #0x30]
  
  libswiftCore.dylib`_swift_release_:
      0x104e18d1c &lt;+180&gt;: bl     0x104e1a37c               ; bool swift::RefCounts&lt;swift::SideTableRefCountBits&gt;::doDecrement&lt;(swift::PerformDeinit)1&gt;(unsigned int)
  -&gt;  0x104e18d20 &lt;+184&gt;: ldp    x29, x30, [sp], #0x10. //Thread 1 error: EXC_BAD_ACCESS (code = 2, address = 0x16d0f3ff0)
      0x104e18d24 &lt;+188&gt;: ret    
  </code></pre>
  
  <p>Does someone have idea how to resolve it?</p>
  
  <p>Thanks!</p>