           // got awoken by insert()
           assert(threadLock.state == .Pointer && threadLock.pointer == buffer,
                  "Unexpected Semaphore state \(threadLock.state) in \(__FUNCTION__)")
-          threadLock.setState(.Done)
-          threadLock.pointer = nil
-          SemaphorePool.Return(threadLock)
-
-          let element = buffer.move()
-          buffer.dealloc(1)
-          return element
+          defer {
+            threadLock.setState(.Done)
+            threadLock.pointer = nil
+            SemaphorePool.Return(threadLock)
+          }
+
+          return buffer.move()
         }
 
       case .Select, .DoubleSelect, .Invalidated, .Done:

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

    threadLock.wait()

    // got awoken
    defer {
      threadLock.setState(.Done)
      threadLock.pointer = nil
      SemaphorePool.Return(threadLock)
    }

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

    case .Pointer:
      assert(threadLock.pointer == &newElement)
      // the message was succesfully passed.
      return threadLock.pointer == &newElement

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