title: What is causing this memory leak in iOS
body: <p>I realize this question has been asked before, but I'm not sure how it applies in my scenario. Using instruments, I have discovered a memory leak, but I'm not sure how to go about correcting the leak. The leak occurs on the <code>OSStatus status</code> line.</p>
  
  <pre><code>// Set up a query dictionary with the base query attributes: item type (generic), username, and service
  NSArray *keys = [[NSArray alloc] initWithObjects: (__bridge_transfer NSString *) kSecClass, kSecAttrAccount, kSecAttrService, nil];
  NSArray *objects = [[NSArray alloc] initWithObjects: (__bridge_transfer NSString *) kSecClassGenericPassword, username, serviceName, nil];
  NSMutableDictionary *query = [[NSMutableDictionary alloc] initWithObjects: objects forKeys: keys];
  // First do a query for attributes, in case we already have a Keychain item with no password data set.
  // One likely way such an incorrect item could have come about is due to the previous (incorrect)
  // version of this code (which set the password as a generic attribute instead of password data).
  NSMutableDictionary *attributeQuery = [query mutableCopy];
  [attributeQuery setObject: (id) kCFBooleanTrue forKey:(__bridge_transfer id) kSecReturnAttributes];
  CFTypeRef attrResult = NULL;
  OSStatus status = SecItemCopyMatching((__bridge  CFDictionaryRef) attributeQuery, (CFTypeRef *) &amp;attrResult); &lt;--- Here is the leak
  NSLog(@"SFHFKeychainUtils =&gt; getPasswordForUsername =&gt; status =&gt; %d\n", status);
  NSLog(@"SFHFKeychainUtils =&gt; getPasswordForUsername =&gt; attrResult =&gt; %@\n", attrResult);
  NSLog(@"SFHFKeychainUtils =&gt; getPasswordForUsername =&gt; attributeQuery =&gt; %@\n", attributeQuery);
  if (status != noErr) {
      // No existing item found--simply return nil for the password
      if (error != nil &amp;&amp; status != errSecItemNotFound) {
          //Only return an error if a real exception happened--not simply for "not found."
          *error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];
      }
      return nil;
  }
  </code></pre>