Permalink
Browse files

Release CFDictionaryRef instances correctly. Fixes #3

CFDictionaryRef instances (cast from NSDictionary with __bridge_retained) were not being released, which caused leaks.
  • Loading branch information...
1 parent 1ad4912 commit 44723892f1b4e86299ffaead9e81bdb2cc1378d3 @rsieiro committed Feb 26, 2012
Showing with 14 additions and 4 deletions.
  1. +14 −4 TwitterDemo/Twitter/RSTwitterEngine.m
View
18 TwitterDemo/Twitter/RSTwitterEngine.m
@@ -97,7 +97,9 @@ - (void)removeOAuthTokenFromKeychain
nil];
// If there's a token stored for this user, delete it
- SecItemDelete((__bridge_retained CFDictionaryRef) keychainQuery);
+ CFDictionaryRef query = (__bridge_retained CFDictionaryRef) keychainQuery;
+ SecItemDelete(query);
+ CFRelease(query);
}
- (void)storeOAuthTokenInKeychain
@@ -113,7 +115,9 @@ - (void)storeOAuthTokenInKeychain
CFTypeRef resData = NULL;
// If there's a token stored for this user, delete it first
- SecItemDelete((__bridge_retained CFDictionaryRef) keychainQuery);
+ CFDictionaryRef query = (__bridge_retained CFDictionaryRef) keychainQuery;
+ SecItemDelete(query);
+ CFRelease(query);
// Build the token dictionary
NSMutableDictionary *tokenDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@@ -128,7 +132,9 @@ - (void)storeOAuthTokenInKeychain
// Add the token data to the keychain
// Even if we never use resData, replacing with NULL in the call throws EXC_BAD_ACCESS
- SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *) &resData);
+ query = (__bridge_retained CFDictionaryRef) keychainQuery;
+ SecItemAdd(query, (CFTypeRef *) &resData);
+ CFRelease(query);
}
- (void)retrieveOAuthTokenFromKeychain
@@ -146,7 +152,9 @@ - (void)retrieveOAuthTokenFromKeychain
CFTypeRef resData = NULL;
// Get the token dictionary from the keychain
- if (SecItemCopyMatching((__bridge_retained CFDictionaryRef) keychainQuery, (CFTypeRef *) &resData) == noErr)
+ CFDictionaryRef query = (__bridge_retained CFDictionaryRef) keychainQuery;
+
+ if (SecItemCopyMatching(query, (CFTypeRef *) &resData) == noErr)
{
NSData *resultData = (__bridge_transfer NSData *)resData;
@@ -162,6 +170,8 @@ - (void)retrieveOAuthTokenFromKeychain
}
}
}
+
+ CFRelease(query);
}
#pragma mark - OAuth Authentication Flow

0 comments on commit 4472389

Please sign in to comment.