提问人:Berte Colin 提问时间:4/11/2023 最后编辑:Berte Colin 更新时间:4/17/2023 访问量:121
为什么 AFNetWork 在 URLSession:dataTask:didReceiveResponse:completionHandler 中崩溃
Why AFNetWork crashes in URLSession:dataTask:didReceiveResponse:completionHandler
问:
我的应用程序在 AFURLSessionManager.m 的 URLSession:dataTask:didReceiveResponse:completionHandler 的第 4 行崩溃,似乎 self 对象被释放了,Func 是我们添加回调的地方:
- (void) Func:(AFHTTPSessionManager *)sessionManager {
@weakify(self);
[sessionManager setDataTaskDidReceiveResponseBlock:^NSURLSessionResponseDisposition(NSURLSession * _Nonnull session, NSURLSessionDataTask * _Nonnull dataTask, NSURLResponse * _Nonnull response) {
@strongify(self);
if (!self) {
return NSURLSessionResponseCancel;
}
......
}
......
}
- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
{
1 NSURLSessionResponseDisposition disposition = NSURLSessionResponseAllow;
2
3 if (self.dataTaskDidReceiveResponse) {
4 disposition = self.dataTaskDidReceiveResponse(session, dataTask, response);(line 1115 of original m file)
5 }
6
7 }
崩溃堆栈:
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x58fe10485d20
Thread 13 Crashed:
0 libobjc.A.dylib 0x000000019d5f9c20 objc_msgSend
1 APPInfo 0x0000000105204e0c -[AFURLSessionManager URLSession:dataTask:didReceiveResponse:completionHandler:] at AFURLSessionManager.m:1115:28
2 CFNetwork 0x00000001a54c9934 CFURLConnectionCreateWithProperties
3 Foundation 0x19e75f2f0 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__
4 Foundation 0x19e7334a0 -[NSBlockOperation main]
5 Foundation 0x19e733430 __NSOPERATION_IS_INVOKING_MAIN__
6 Foundation 0x19e6f47d8 -[NSOperation start]
7 Foundation 0x19e6f450c __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__
8 Foundation 0x19e6f9c20 __NSOQSchedule_f
9 libdispatch.dylib 0x1ab8c1114 _dispatch_block_async_invoke2
10 libdispatch.dylib 0x1ab8b1fdc _dispatch_client_callout
11 libdispatch.dylib 0x1ab8b546c _dispatch_continuation_pop
12 libdispatch.dylib 0x1ab8b4ad4 _dispatch_async_redirect_invoke
13 libdispatch.dylib 0x1ab8c3a6c _dispatch_root_queue_drain
14 libdispatch.dylib 0x1ab8c4284 _dispatch_worker_thread2
15 libsystem_pthread.dylib 0x1f133bdbc _pthread_wqthread
16 libsystem_pthread.dylib 0x1f133bb98 start_wqthread
答:
0赞
Annett Schwarze
4/12/2023
#1
很可能是在调用完成处理程序时释放了对象。要确定,需要更多的代码片段。
您应该检查是否位于完成处理程序的开头。
或者,您应该确保对实例的引用是强保留的,因此在调用完成处理程序之前不会释放它。self
nil
要确保引用在完成处理程序启动之前是弱的,然后在执行时保持强引用的一种模式是这样的:
__weak typeof(self) weakSelf = self;
[someObj someCall:^{
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf == nil) return;
...
}];
希望这会有所帮助。
评论
0赞
Berte Colin
4/13/2023
我们确实添加了代码:@weakify(self);[sessionManager setDataTaskDidReceiveResponseBlock:^NSURLSessionResponseDisposition(NSURLSession * _Nonnull session, NSURLSessionDataTask * _Nonnull dataTask, NSURLResponse * _Nonnull response) { @strongify(self); if (!self) { return NSURLSessionResponseCancel; }
评论