Skip to content

Commit 5f24837

Browse files
committed
solve urlSession leak
1 parent 250c801 commit 5f24837

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

QiniuDemo/Podfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ target "QiniuDemo" do
66
end
77

88
target "QiniuDemoTests" do
9-
platform :ios, "7.0"
10-
pod 'Qiniu',:path => '../'
9+
# platform :ios, "7.0"
10+
# pod 'Qiniu',:path => '../'
1111
end

QiniuSDK/Http/QNSessionManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
7474
_sessionStatistics.bytesSent = task.countOfBytesSent;
7575
_sessionStatistics.bytesTotal = task.countOfBytesExpectedToSend;
7676
self.completeBlock(_responseData, task.response, error, _sessionStatistics);
77+
[session finishTasksAndInvalidate];
7778
}
7879

7980
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics API_AVAILABLE(ios(10.0)) {

QiniuSDK/Http/UrlProtocol/NSURLRequest+QNRequest.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,8 @@ - (void)setQn_domain:(NSString *)qn_domain{
100100
} else {
101101
[self setValue:nil forHTTPHeaderField:kQNURLReuestHostKey];
102102
}
103-
104-
NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];
105-
NSString *identifier = [NSString stringWithFormat:@"%lf%@", timestamp, qn_domain];
103+
104+
NSString *identifier = [NSString stringWithFormat:@"%p-%@", &self, qn_domain];
106105
[self setQn_identifier:identifier];
107106
}
108107

QiniuSDK/Http/UrlProtocol/QNURLProtocol.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#import <objc/runtime.h>
1515

1616
@interface QNRequestInfo : NSObject
17-
@property(nonatomic, strong)NSURLSession *session;
18-
@property(nonatomic, strong)NSURLSessionDataTask *task;
17+
@property(nonatomic, weak)NSURLSession *session;
18+
@property(nonatomic, weak)NSURLSessionDataTask *task;
1919
@end
2020
@implementation QNRequestInfo
2121
@end
@@ -88,7 +88,7 @@ - (void)qn_setSession:(NSURLSession *)session task:(NSURLSessionDataTask *)task{
8888
info.task = task;
8989
[[QNRequestInfoManager share] setRequestInfo:info forRequest:self];
9090
}
91-
- (void)qn_requestComplete{
91+
- (void)qn_requestRemoveTask{
9292
[[QNRequestInfoManager share] removeRequestInfoForRequest:self];
9393
}
9494
- (BOOL)qnHttps_shouldInit{
@@ -154,6 +154,7 @@ + (BOOL)canInitWithRequest:(NSURLRequest *)request {
154154
&& address.ipValue && address.ipValue.length > 0) {
155155
return YES;
156156
} else {
157+
[request qn_requestRemoveTask];
157158
return NO;
158159
}
159160
}
@@ -225,7 +226,7 @@ - (void)didSendBodyData:(int64_t)bytesSent
225226
- (void)didFinish {
226227

227228
[self.client URLProtocolDidFinishLoading:self];
228-
[self.request qn_requestComplete];
229+
[self.request qn_requestRemoveTask];
229230
}
230231

231232
- (void)didLoadData:(nonnull NSData *)data {

0 commit comments

Comments
 (0)