NSURLConnection causes bad access crash

梦想与她 提交于 2019-12-10 17:24:40

问题


I have been fighting with this for the past 48 hours and searching all forums with no use. I am using NSURLConnection to asynchronously download a series of web files. It randomly crashes after downloading a few files, the number of which is not fixed.

Please see the call stack below:

#0 0x35b634f4 in objc_msgSend
#1 0x3527c2ca in CFEqual
#2 0x3532e750 in __CFBasicHashStandardEquateKeys
#3 0x35330514 in ___CFBasicHashFindBucket_Linear
#4 0x3527c07c in CFBasicHashFindBucket
#5 0x3527bf5c in CFDictionaryGetValue
#6 0x31d15cb4 in HTTPMessage::copyConstantHeaderFieldValue
#7 0x31d6526c in URLRequest::copyConstantHeaderFieldValue
#8 0x31d606f8 in HTTPProtocol::getCacheStoragePolicy
#9 0x31d62cd0 in HTTPProtocol::updateForHeader
#10 0x31d64524 in HTTPProtocol::performHeaderRead
#11 0x31d64bf8 in HTTPProtocol::httpReadStreamEvent
#12 0x352c4302 in _signalEventSync
#13 0x352c427c in _cfstream_shared_signalEventSync
#14 0x352ed294 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
#15 0x352ef0e2 in __CFRunLoopDoSources0
#16 0x352f0058 in __CFRunLoopRun
#17 0x352970c2 in CFRunLoopRunSpecific
#18 0x35296fd0 in CFRunLoopRunInMode
#19 0x30a1f31c in +[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:]
#20 0x309ef618 in -[NSThread main]
#21 0x30a75146 in __NSThread__main__

After seeing a previous thread on this on stackoverflow it is indeed a problem with redirect requests being sent. The solution suggested there was to implement

- (NSURLRequest *)connection:(NSURLConnection *)connection 
             willSendRequest:(NSURLRequest *)request 
            redirectResponse:(NSURLResponse *) redirectResponse
{
 return request;
}

I did that but still the crash is there. Also the behaviour before crash is that, willSendRequest is being called twice and no other delegate method is called for this request. It crashes internally in NSURLResponse.


回答1:


This may helpful: Asynchronous NSURLConnection Throws EXC_BAD_ACCESS

For example:

I've fixed my problem with NSZombieEnabled flag. My problem was supplied NSURL was autorelease object. So the URL object deallocated at some specific time, and accessing to it failed in internal API code.



来源:https://stackoverflow.com/questions/3111051/nsurlconnection-causes-bad-access-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!