Sdwebimage: EXC_BAD_ACCESS

Created on 18 Aug 2016  路  8Comments  路  Source: SDWebImage/SDWebImage

New Issue Checklist

| Info | Value |
| --- | --- |
| Platform Name | e.g. ios |
| Platform Version | e.g. 9.3 |
| SDWebImage Version | e.g. 3.8.1 |
| Integration Method | e.g. cocoapods |
| Xcode Version | e.g. Xcode 7.3 |
| Repro rate | e.g. sometimes 30% |
| Repro with our demo prj | e.g. does it happen with our demo project? |
| Demo project link | e.g. link to a demo project that highlights the issue |

Issue Description and Steps

Please fill in the detailed description of the issue (full output of any stack trace, compiler error, ...) and the steps to reproduce the issue.

qq20160818-0 2x
qq20160818-1 2x

crash

Most helpful comment

I found some crash was by calling block like this:

if (self.completedBlock) {
  self.completedBlock(nil, nil, error, YES);
}

the NSURLSession delegate was not calling in the main thread, and the other thread cancel the operation.
After checking self.completedBlock, operation was canceled and self.completedBlock assigned to nil, calling nil as a block will crash.
And the crash address will always be 0x10.

this can be fix like this:

BlockType completedBlock = self.completedBlock
if (completedBlock) {
  completedBlock(nil, nil, error, YES);
}

All 8 comments

Can you supply a url of your image?

Picture in the news list , Seems to have no relationship with url , 2.7.6 the following has never happened .

@lixiang1994 what's the description printed in the console? It should say something about the error

I found some crash was by calling block like this:

if (self.completedBlock) {
  self.completedBlock(nil, nil, error, YES);
}

the NSURLSession delegate was not calling in the main thread, and the other thread cancel the operation.
After checking self.completedBlock, operation was canceled and self.completedBlock assigned to nil, calling nil as a block will crash.
And the crash address will always be 0x10.

this can be fix like this:

BlockType completedBlock = self.completedBlock
if (completedBlock) {
  completedBlock(nil, nil, error, YES);
}

Can someone create a PR with those fixes? I don't know to which pieces of code they apply

This should be fixed in SD 4.0. In 4.0 we call the completion block in main thread.(https://github.com/rs/SDWebImage/blob/master/SDWebImage/SDWebImageDownloaderOperation.m#L534)
And also, it should be fixed in 3.8.2.(https://github.com/rs/SDWebImage/blob/3.8.2/SDWebImage/SDWebImageDownloaderOperation.m#L407)

I checkout the 3.7.6 version and agree that there may be a race condition when you enter the block and prepare to call the block, but another thread cancel the task(which will call self.completedBlock = nil;)

The relative code is here: https://github.com/rs/SDWebImage/blob/3.7.6/SDWebImage/SDWebImageDownloaderOperation.m#L244

This fix will work on 3.7.6 version because the block is retrieved in advance and will not be affected by that self.completedBlock = nil;(block property attribute is copy). But since we released 3.7.6, maybe someone could checkout 3.7.6 tag and create an bug fix release?

Need we release a patch release based on 3.7.6 such as 3.7.7 ? To do this, we need a extra branch such as 3.7.x-compat checked out from 3.7.6 tag and merge the fix pull request(I create one here) to that branch and finally add an new 3.7.7 tag because 3.8.x drop the iOS 5-6 support. (Oh great the backwards compatibility 馃槥 )

@dreampiggy that is the ideal case :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ricardo1980 picture Ricardo1980  路  6Comments

doingy picture doingy  路  4Comments

ericeddy picture ericeddy  路  6Comments

MagLiC picture MagLiC  路  3Comments

halilyuce picture halilyuce  路  4Comments