From b6819711167889944497f3144e465f46f8b12514 Mon Sep 17 00:00:00 2001 From: Evan Long Date: Thu, 15 Sep 2011 00:58:41 -0700 Subject: [PATCH] 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. --- Example/Classes/AFGowallaAPIClient.m | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Example/Classes/AFGowallaAPIClient.m b/Example/Classes/AFGowallaAPIClient.m index 9875d7913d..d8ee3118a4 100644 --- a/Example/Classes/AFGowallaAPIClient.m +++ b/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; }