Skip to content

Commit

Permalink
Merge pull request AFNetworking#4256 from classdojo/cd-master
Browse files Browse the repository at this point in the history
Fix crashes and memory leaks
  • Loading branch information
SlaunchaMan authored Sep 24, 2018
2 parents 59e92b1 + 9bb8ea7 commit 1bd73c7
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 72 deletions.
10 changes: 9 additions & 1 deletion AFNetworking/AFURLSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,15 @@ NS_ASSUME_NONNULL_BEGIN
@param cancelPendingTasks Whether or not to cancel pending tasks.
*/
- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks;
- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks DEPRECATED_ATTRIBUTE;

/**
Invalidates the managed session, optionally canceling pending tasks and optionally resets given session.
@param cancelPendingTasks Whether or not to cancel pending tasks.
@param resetSession Whether or not to reset the session of the manager.
*/
- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks resetSession:(BOOL)resetSession;

///-------------------------
/// @name Running Data Tasks
Expand Down
33 changes: 27 additions & 6 deletions AFNetworking/AFURLSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ + (void)load {
8) Set the current class to the super class, and repeat steps 3-8
*/
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession * session = [NSURLSession sessionWithConfiguration:configuration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnonnull"
NSURLSessionDataTask *localDataTask = [session dataTaskWithURL:nil];
Expand Down Expand Up @@ -518,8 +518,6 @@ - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)config
self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = 1;

self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];

self.responseSerializer = [AFJSONResponseSerializer serializer];

self.securityPolicy = [AFSecurityPolicy defaultPolicy];
Expand All @@ -533,17 +531,20 @@ - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)config
self.lock = [[NSLock alloc] init];
self.lock.name = AFURLSessionManagerLockName;

__weak typeof(self) weakSelf = self;
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {

__strong typeof(weakSelf) strongSelf = weakSelf;
for (NSURLSessionDataTask *task in dataTasks) {
[self addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil];
[strongSelf addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil];
}

for (NSURLSessionUploadTask *uploadTask in uploadTasks) {
[self addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil];
[strongSelf addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil];
}

for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
[self addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil];
[strongSelf addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil];
}
}];

Expand All @@ -556,6 +557,19 @@ - (void)dealloc {

#pragma mark -

- (NSURLSession *)session {

@synchronized (self) {
if (!_session) {
_session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];
}
}
return _session;
}

#pragma mark -


- (NSString *)taskDescriptionForSessionTasks {
return [NSString stringWithFormat:@"%p", self];
}
Expand Down Expand Up @@ -712,11 +726,18 @@ - (NSArray *)downloadTasks {
#pragma mark -

- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks {
[self invalidateSessionCancelingTasks:cancelPendingTasks resetSession:NO];
}

- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks resetSession:(BOOL)resetSession {
if (cancelPendingTasks) {
[self.session invalidateAndCancel];
} else {
[self.session finishTasksAndInvalidate];
}
if (resetSession) {
self.session = nil;
}
}

#pragma mark -
Expand Down
Loading

0 comments on commit 1bd73c7

Please sign in to comment.