0

I'm using the SDWebImageDownloader library to download images asynchronously. The problem i'm having is when I click the back button before the images finish download the app is crashing on the following line in the SDWebImage Class:

if([delegate respondsToSelector:@selector(imageDownloaderDidFinish:)])

This is how i'm using it in my code:

sdDownloader = [[SDWebImageDownloader downloaderWithURL:headerImgURL delegate:self]retain];

What is causing it to crash? I'm retaining it and i'm not releasing it anywhere.

user1417302
  • 411
  • 3
  • 9
  • 22

1 Answers1

0

When you say you are retaining "it", what do you mean by "it"? If it's crashing with EXC_BAD_ACCESS when you try to send a message to delegate, it's likely that delegate is a dangling pointer because it's being released prematurely. What is retaining delegate? What is releasing delegate?

Jim
  • 72,985
  • 14
  • 101
  • 108
  • i'm retaining sdDownloader and i'm not releasing the sdDownloader object anywhere so there's no way it is being released prematurely. – user1417302 Jul 27 '12 at 13:54
  • I'm not talking about `sdDownloader`, I'm talking about `delegate`. – Jim Jul 27 '12 at 13:55
  • well i'm implementing all of the SDWebImageDownloader delegate methods, so i'm not sure how to release or retain a delegate – user1417302 Jul 27 '12 at 13:56
  • I'm not talking about implementing delegate methods, I'm talking about retaining the delegate. You are passing `self` in as the delegate. What is retaining this object? What is releasing it? – Jim Jul 27 '12 at 13:58
  • If nothing is retaining it, then it will be deallocated as soon as it goes out of scope. That will leave `sdDownloader` with a dangling pointer to deallocated memory where it thinks its delegate will be. When it's trying to send a message to its delegate, it's dereferencing this garbage pointer, hence the crash. – Jim Jul 27 '12 at 14:00
  • You retain objects by passing a `retain` message to them. I suggest you read [Basic Memory Management Rules](https://developer.apple.com/library/ios/#documentation/cocoa/conceptual/memorymgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-SW1) in the official documentation. – Jim Jul 27 '12 at 14:04