           OSSpinLockUnlock(&lock)
           let threadLock = SemaphorePool.Obtain()
           let buffer = UnsafeMutablePointer<T>.alloc(1)
+          defer { buffer.dealloc(1) }
           threadLock.setState(.Pointer)
           threadLock.pointer = UnsafeMutablePointer(buffer)
           writer.sem.selection = writer.sel.withSemaphore(threadLock)

    threadLock.wait()

    // got awoken
    let state = threadLock.state
    let match = threadLock.pointer == &newElement
    threadLock.setState(.Done)
    threadLock.pointer = nil
    SemaphorePool.Return(threadLock)

    switch state
    {
    case .Done:
      // thread was awoken by close() and put() has failed
      return false

    case .Pointer where match:
      // the message was succesfully passed.
      return match

    default:
      preconditionFailure("Unexpected Semaphore state \(state) after wait in \(__FUNCTION__)")
    }
  }

          // got awoken by insert()
          assert(threadLock.state == .Pointer && threadLock.pointer == buffer,
                 "Unexpected Semaphore state \(threadLock.state) in \(__FUNCTION__)")
          defer {
            threadLock.setState(.Done)
            threadLock.pointer = nil
            SemaphorePool.Return(threadLock)
          }

          return buffer.move()
        }

      case .Select, .DoubleSelect, .Invalidated, .Done:
