Skip to content

Loading…

Fixing a leak as a result of a race condition in the sample Gowalla client #28

Merged
merged 1 commit into from

2 participants

@evanlong

The allocation for the shared client could occur twice resulting in a leak. Two
threads could pass the nil check. One would acquire the lock and create the
sharedClient. The second thread would eventually get the lock and also acquire
a sharedClient.

Evan Long The allocation for the shared client could occur twice resulting in a…
… leak. Two

threads could pass the nil check. One would acquire the lock and create the
sharedClient. The second thread would eventually get the lock and also acquire
a sharedClient.
b681971
@mattt mattt merged commit 78f1928 into AFNetworking:master
@mattt

Could've sworn I'd replaced all of the static initializers with dispatch_once. Anyway, thanks for the keen eye, as always. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 15, 2011
  1. The allocation for the shared client could occur twice resulting in a…

    Evan Long committed
    … leak. Two
    
    threads could pass the nil check. One would acquire the lock and create the
    sharedClient. The second thread would eventually get the lock and also acquire
    a sharedClient.
Showing with 4 additions and 5 deletions.
  1. +4 −5 Example/Classes/AFGowallaAPIClient.m
View
9 Example/Classes/AFGowallaAPIClient.m
@@ -32,11 +32,10 @@
@implementation AFGowallaAPIClient
+ (id)sharedClient {
- if (_sharedClient == nil) {
- @synchronized(self) {
- _sharedClient = [[self alloc] init];
- }
- }
+ static dispatch_once_t oncePredicate;
+ dispatch_once(&oncePredicate, ^{
+ _sharedClient = [[self alloc] init];
+ });
return _sharedClient;
}
Something went wrong with that request. Please try again.