ASIHTTPRequest block释放问题-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

ASIHTTPRequest block释放问题

2016-07-20 11:56:02 2380 1

最近在iOS4.3.3的机器上发现用asi的block会有很大概率产生crash.
用了ARC
crash表现情况为:bool _WebTryThreadLock(bool), 0x20a0fb20: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
2 libsystem_c.dylib 0x3388b727 _sigtramp + 34;
3 WebCore 0x31d363bf _ZL17_WebTryThreadLockb + 102;
4 WebCore 0x31d381b1 WebThreadLock + 52;
5 UIKit 0x3317265f -[UITextView dealloc] + 30;
6 CoreFoundation 0x334afc43 -[NSObject(NSObject) release] + 30;
7 snstaoban 0x00190ffb __arclite_objc_storeStrong + 54;
8 snstaoban 0x0001e5ab -[ShareInputView .cxx_destruct] + 106;
9 libobjc.A.dylib 0x3398d961 object_cxxDestructFromClass + 52;
10 libobjc.A.dylib 0x3398fb15 object_cxxDestruct + 12;
11 libobjc.A.dylib 0x3398fb25 objc_destructInstance + 12;
12 libobjc.A.dylib 0x3398d917 object_dispose + 26;
13 CoreFoundation 0x334afee5 -[NSObject(NSObject) dealloc] + 24;
14 UIKit 0x32ffbec7 -[UIView dealloc] + 382;
15 UIKit 0x33058739 -[UIImageView dealloc] + 64;
16 snstaoban 0x0001d485 -[ShareInputView dealloc] + 120;
17 CoreFoundation 0x334afc43 -[NSObject(NSObject) release] + 30;
18 UIKit 0x32fe85f5 -[UIView(Hierarchy) removeFromSuperview] + 364;
19 UIKit 0x32ffbde3 -[UIView dealloc] + 154;
20 CoreFoundation 0x334afc43 -[NSObject(NSObject) release] + 30;
21 UIKit 0x3305400f -[UIViewController dealloc] + 174;
22 snstaoban 0x00070467 -[RootViewController dealloc] + 238;
23 snstaoban 0x00015f21 -[ShareSetViewController dealloc] + 120;
24 CoreFoundation 0x334afc43 -[NSObject(NSObject) release] + 30;
25 snstaoban 0x00190f29 __arclite_objc_release + 20;
26 snstaoban 0x00178723 __destroy_helper_block_160 + 10;
27 libsystem_blocks.dylib 0x365a688f _Block_release + 58;
28 CoreFoundation 0x334e4285 -[__NSMallocBlock release] + 8;
29 snstaoban 0x00190f29 __arclite_objc_release + 20;
30 snstaoban 0x000733a9 __destroy_helper_block_64 + 12;
31 libsystem_blocks.dylib 0x365a688f _Block_release + 58;
32 CoreFoundation 0x334e4285 -[__NSMallocBlock release] + 8;
33 CoreFoundation 0x334b01a1 CFRelease + 68;
34 CoreFoundation 0x334b5ba9 -[__NSArrayM dealloc] + 92;
35 CoreFoundation 0x334afc43 -[NSObject(NSObject) release] + 30;
36 CoreFoundation 0x334b01a1 CFRelease + 68;
37 CoreFoundation 0x334b2ebb _CFAutoreleasePoolPop + 146;
38 libdispatch.dylib 0x30df06a5 _dispatch_worker_thread2 + 372;
39 libsystem_c.dylib 0x33881591 _pthread_wqthread + 264;
40 libsystem_c.dylib 0x33881bc4 _init_cpu_capabilities + 4294967295||dep=58,idx=57
原因是asi的completionBlock和failureBlock在非主线程(Thread 1 name: Dispatch queue: com.apple.root.low-priority)被释放,然后controller, view接着在这个线程被释放导致crash.
测试了5.0一上也是在非主线程被释放, 但是不会导致crash.
但是4.3.3的模拟器block是在主线程被释放.
于是查看asi的代码发现一个trick.
在dealloc里有

#if NS_BLOCKS_AVAILABLE
[self releaseBlocksOnMainThread];
#endif

if NS_BLOCKS_AVAILABLE

  • (void)releaseBlocksOnMainThread
    {
    NSMutableArray *blocks = [NSMutableArray array];
    if (completionBlock) {

    [blocks addObject:completionBlock];
    [completionBlock release];
    completionBlock = nil;

    }
    if (failureBlock) {

    [blocks addObject:failureBlock];
    [failureBlock release];
    failureBlock = nil;

    }
    ....
    [[self class] performSelectorOnMainThread:@selector(releaseBlocks:) withObject:blocks waitUntilDone:[NSThread isMainThread]];
    }

// Always called on main thread

  • (void)releaseBlocks:(NSArray *)blocks
    {

    // Blocks will be released when this method exits

    }

endif

疑问:
1.blocks变量应该在主线程被释放还是releaseBlocksOnMainThread所在线程?有文档能证明么?
2.releaseBlocks为什么里面什么都不干呢?

另外:我测试解决4.3.3的blockcrash问题就是改掉

  • (void)releaseBlocks:(NSMutableArray *)blocks
    {

    // Blocks will be released when this method exits
    [blocks removeAllObjects];//解决4.3.3子线程释放block的bug

    }

不知道这样做是否合理, 如果是合理的为什么作者不这样做呢?

取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:58:47

    首先,你改了这个方法原理上并不能解决blocks变量被过度释放的问题,因为你只是对blocks这数组的元素做了操作。
    另,回答你两个问题:
    1,blocks变量一定是在主线程释放的,就是避免多线程并发时的冲突,所以才会

    [[self class] performSelectorOnMainThread:@selector(releaseBlocks:) withObject:blocks waitUntilDone:[NSThread isMainThread]];
    2,我还真没想到为什么他是空的,我再研究一下~~~

    另外,建议你把创建请求,处理block的代码都贴上来,因为现在看现象我觉得是你处理Block的时候有问题,不是ASI的问题

    0 0
相关问答

1

回答

Query_block

2022-04-29 09:40:41 1057浏览量 回答数 1

1

回答

parition和block有什么关联关系?

2021-12-06 00:36:34 93浏览量 回答数 1

1

回答

springboot mybatis通过xml方式配置sql应该弄什么?

2021-10-28 19:29:45 206浏览量 回答数 1

1

回答

my.request 错误代码 error:4

2020-10-26 10:03:52 209浏览量 回答数 1

1

回答

sphinx配置sql_attr_multi:报错

2020-06-05 23:58:54 465浏览量 回答数 1

1

回答

mysqli_stmt :: bind_param():类型定义字符串中的元素数量与绑定?mysql

2020-05-16 22:22:31 209浏览量 回答数 1

1

回答

弹性高性能计算E-HPC自动伸缩管理API有哪些?

2020-03-23 18:14:10 481浏览量 回答数 1

0

回答

请问使用CommonRequest访问EDAS的API时,版本号在哪里能找到?

2018-06-05 11:03:01 2057浏览量 回答数 0

1

回答

使用jquery给img绑定error事件的问题

2016-03-23 14:12:34 1711浏览量 回答数 1

1

回答

相关mysql查询归组的问题

2016-02-12 17:17:26 1891浏览量 回答数 1
+关注
0
文章
14879
问答
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载