title: OS X Crash: pointer being freed was not allocated
body: <p>Need a help to understand the real reason of a crash. I've made the app that should work 24/7. It reads and prepares some data from an equipment and creates a web-server to send data to the probe.
  The crash happens after a various amount of time, so I can't really reproduce it in the simulator.</p>
  
  <p>Here are the last crash file details:</p>
  
  <blockquote>
    <p>Time Awake Since Boot: 110000 seconds</p>
    
    <p>System Integrity Protection: enabled</p>
    
    <p>Crashed Thread:        37  Dispatch queue: com.apple.root.default-qos</p>
    
    <p>Exception Type:        EXC_CRASH (SIGABRT)</p>
    
    <p>Exception Codes:       0x0000000000000000, 0x0000000000000000</p>
    
    <p>Exception Note:        EXC_CORPSE_NOTIFY</p>
    
    <p>Dispatch Thread Soft Limit Reached: 64 (too many dispatch threads blocked in synchronous operations)</p>
    
    <p>Application Specific Information:
    abort() called</p>
    
    <p>*** error for object 0x247032000: pointer being freed was not allocated</p>
  </blockquote>
  
  <p>Details for Thread 37:</p>
  
  <blockquote>
    <p>Thread 37 Crashed:: Dispatch queue: com.apple.root.default-qos</p>
    
    <p>0   libsystem_c.dylib              0x00007fff8dab3298 usleep$NOCANCEL + 0</p>
    
    <p>1   libsystem_c.dylib              0x00007fff8dae16e9 abort + 139</p>
    
    <p>2   libsystem_malloc.dylib         0x00007fff965fe041 free + 425</p>
    
    <p>3   libswiftCore.dylib             0x0000000106fed219 $Ss19_SwiftStringStorageCfD + 9</p>
    
    <p>4   libswiftCore.dylib             0x0000000106ffba00 _swift_release_dealloc + 16</p>
  </blockquote>
  
  <p>...</p>
  
  <blockquote>
    <p>46  org.cocoapods.GCDWebServer     0x0000000106c67316 -[GCDWebServerConnection(Subclassing) processRequest:completion:] + 128</p>
    
    <p>47  org.cocoapods.GCDWebServer     0x0000000106c6392e -[GCDWebServerConnection _startProcessingRequest] + 146</p>
    
    <p>48  org.cocoapods.GCDWebServer     0x0000000106c64e13 __45-[GCDWebServerConnection _readRequestHeaders]_block_invoke + 1781</p>
    
    <p>49  org.cocoapods.GCDWebServer     0x0000000106c65935 __64-[GCDWebServerConnection(Read) readHeaders:withCompletionBlock:]_block_invoke + 290</p>
    
    <p>50  org.cocoapods.GCDWebServer     0x0000000106c65613 __68-[GCDWebServerConnection(Read) readData:withLength:completionBlock:]_block_invoke + 307</p>
    
    <p>51  libdispatch.dylib              0x00007fff8e73c7a8 __dispatch_read_block_invoke_252 + 39</p>
    
    <p>52  libdispatch.dylib              0x00007fff8e72a93d _dispatch_call_block_and_release + 12</p>
    
    <p>53  libdispatch.dylib              0x00007fff8e71f40b _dispatch_client_callout + 8</p>
    
    <p>54  libdispatch.dylib              0x00007fff8e72329b _dispatch_root_queue_drain + 1890</p>
    
    <p>55  libdispatch.dylib              0x00007fff8e722b00 _dispatch_worker_thread3 + 91</p>
    
    <p>56  libsystem_pthread.dylib        0x00007fff8ea934de _pthread_wqthread + 1129</p>
    
    <p>57  libsystem_pthread.dylib        0x00007fff8ea91341 start_wqthread + 13</p>
  </blockquote>
  
  <p>Code:</p>
  
  <p>This is a code of a singletone that handles the web-server (GCDWebServer). It reads stored in memory data according to an id in an http query</p>
  
  <pre><code>private let queue = DispatchQueue(label: "gcdwebserver_queue")
  
  private func setupServer(){
  
      webServer.delegate = self
      webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self) { (req, completion) in
  
          if let resp = self.response(for: req) {
              return completion(resp)
          }
      }
  
      queue.async {
          self.webServer.start(withPort: 8521, bonjourName: "GCD Web Server")            
      }
  
  }
  </code></pre>
  
  <p>And here is the code of a singleton that calls modbus (C Modbus library) connection (every 30 sec) to a list of devices and reads data:</p>
  
  <pre><code>private let modbusQueue = DispatchQueue(label: "modbus_queue")
  private func initiateTimer() {
      polling()
  
      timer?.invalidate()
      if #available(OSX 10.12, *) {
          timer = Timer.scheduledTimer(withTimeInterval: pollingInterval, repeats: true) { (_) in
              self.polling()
          }
      } else {
          timer = Timer.scheduledTimer(timeInterval: pollingInterval, target: self, selector: #selector(polling), userInfo: nil, repeats: true)
      }
  }
  
  @objc private func polling() {
  
      for device in self.devices {
  
          if !device.isInProcess &amp;&amp; device.isEnabled {
              self.modbusQueue.async {
                  self.connectAndReadValues(device: device)
              }
          }
  
      }
  }
  
  private func connectAndReadValues(device: Device) {
  
      device.isInProcess = true
  
      let connect = device.modbus.connect()
      //handling connection status
      //...
  
      readValues(forDevice: device)
  }
  
  private func readValues(forDevice device: Device){
      //some constants
  
      do {
  
          let registers = try device.modbus.readRegisters(from: startAddress, count: registersCount)
          device.readState = .success
          device.modbus.close()
          //parse and save data to the app memory just as a dictionary. It saves only one small dictionary per device
          parseRegisters(controllerIP: device.modbus.host, vendor: vendor, registers: registers, startAdd: startAddress)
      } catch let error {
          //handling errors
      }
  
      //refreshing interface in the main queue
  }
  </code></pre>