Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition causes imageData to be nil #863

Closed
bpoplauschi opened this issue Aug 14, 2014 · 6 comments
Closed

Race condition causes imageData to be nil #863

bpoplauschi opened this issue Aug 14, 2014 · 6 comments
Labels
Milestone

Comments

@bpoplauschi
Copy link
Member

Thanks to @jdelStrother for this:

Comment 1:

For what it's worth, I ran into this crash on 3.7.0, while rapidly scrolling a tableview containing UIImageViews, each of which was requesting a load from SDWebImage. I ended up at this stack trace :

#0  0x000000010e571f9f in CFRelease ()
#1  0x000000010a499d02 in +[UIImage(MultiFormat) sd_imageOrientationFromImageData:] at /Users/jon/Developer/iphone/Pods/SDWebImage/SDWebImage/UIImage+MultiFormat.m:63
#2  0x000000010a499b20 in +[UIImage(MultiFormat) sd_imageWithData:] at /Users/jon/Developer/iphone/Pods/SDWebImage/SDWebImage/UIImage+MultiFormat.m:34
#3  0x000000010a489960 in -[SDWebImageDownloaderOperation connectionDidFinishLoading:] at /Users/jon/Developer/iphone/Pods/SDWebImage/SDWebImage/SDWebImageDownloaderOperation.m:343
#4  0x000000010b5e536b in __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke ()
#5  0x000000010b498bdb in -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] ()
#6  0x000000010b498aec in -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] ()
#7  0x000000010e381637 in ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke ()
#8  0x000000010e37f802 in ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 ()
#9  0x000000010e590f74 in CFArrayApplyFunction ()
#10 0x000000010e2f23e7 in RunloopBlockContext::perform() ()
#11 0x000000010e2f2217 in MultiplexerSource::perform() ()
#12 0x000000010e2f203a in MultiplexerSource::_perform(void*) ()
#13 0x000000010e579d21 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#14 0x000000010e5795f2 in __CFRunLoopDoSources0 ()
#15 0x000000010e59546f in __CFRunLoopRun ()
#16 0x000000010e594d83 in CFRunLoopRunSpecific ()
#17 0x000000010e59f8c1 in CFRunLoopRun ()
#18 0x000000010a487651 in -[SDWebImageDownloaderOperation start] at /Users/jon/Developer/iphone/Pods/SDWebImage/SDWebImage/SDWebImageDownloaderOperation.m:103
#19 0x000000010b56cc0b in __NSOQSchedule_f ()
#20 0x000000010f8e072d in _dispatch_client_callout ()
#21 0x000000010f8ceeab in _dispatch_async_redirect_invoke ()
#22 0x000000010f8e072d in _dispatch_client_callout ()
#23 0x000000010f8d0b27 in _dispatch_root_queue_drain ()
#24 0x000000010f8d0d12 in _dispatch_worker_thread2 ()
#25 0x000000010fc7cef8 in _pthread_wqthread ()
#26 0x000000010fc7ffb9 in start_wqthread ()

By this point, self.imageData was nil, which is why imageSource ended up as NULL. self.expectedSize was set, however, so didReceiveResponse presumably got called.

Is it a race condition where one thread finishes downloading & calls connectionDidFinishLoading, and then another thread calls reset ?

Comment 2:

Actually, I can reproduce this pretty easily if I put a sleep(1) in connectionDidFinishLoading to exaggerate the race condition, and repeatedly calling sd_setImageWithURL from the main thread.

Possibly connectionDidFinishLoading should take a reference to imageData at the start (like it does with completionBlock), and bail if either are nil? Even that might not be sufficient though - is it ok calling done (and thus reset) at the end of connectionDidFinishLoading, when the main thread might have tried to start a new image loading in the middle of that method?

Comment 3:

Yeah, 3.7.1 fixes the crash, though you get ImageIO: CGImageSourceCreateWithData data parameter is nil in the log. Still slightly concerned about race conditions in the latter half of connectionDidFinishLoading, but I can't reproduce any problems with it.

@bpoplauschi
Copy link
Member Author

Problem originally discussed in #764

@bpoplauschi bpoplauschi added this to the 4.0.0 milestone Aug 14, 2014
@rsanchezsaez
Copy link

I think this is also related to #847, as I believe that one is also because of racing conditions.

Sorry to say but SDWebImage 3.7.1 is really buggy... :-(

@bpoplauschi
Copy link
Member Author

So I say, let's fix it :)

@nishan-guiddit
Copy link

@bpoplauschi has this issue been fixed. I'm facing the same problem as I'm trying to load the images in the Tableview. Application gets crashed and below error log is got

": ImageIO: CGImageSourceCreateWithData data parameter is nil "

Can you please let me know how this can be solved ?

@kiddtaurus
Copy link

@bpoplauschi Whether the bug has be fixed in release 3.7.3?

@bpoplauschi
Copy link
Member Author

Should be fixed in the latest 4.x branch. Should revisit after 4.0 is released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants