     let numFields = readFieldLength()
     var objectReader = ByteTreeObjectReader(reader: self,
                                             numFields: numFields)
+    defer {
+      objectReader.finalize()
+    }
     return T.read(from: &objectReader, numFields: numFields, userInfo: userInfo)
   }
 

  /// advances the reader by one field so that the next field can be read.
  ///
  /// - Parameter index: The index of the field that shall be discarded
  func discardField(index: Int) {
    advanceAndValidateIndex(index)
    reader.discardField()
  }

  deinit {
    // Discard all fields that have not been read
    while nextIndex < numFields {
      discardField(index: nextIndex)

/// Helper object for reading objects out a ByteTree. Keeps track that fields
/// are not read out of order and discards all trailing fields that were present
/// in the binary format but were not handled when reading the object.
struct ByteTreeObjectReader {
  /// The reader that holds a reference to the data from which the object is
  /// read
  private let reader: ByteTreeReader
