20

A have a view controller, and it creates a "downloader" object, which has a reference to the view controller (as a delegate). The downloader calls back the view controller if it successfully downloads the item. This works fine as long as you stay on the view, but if you navigate away before the download is complete I get EXC_BAD_ACCESS. I understand why this is happening, but is there any way to check if an object is still allocated? I tried to test using delegate != nil, and [delegate respondsToSelector:], but it chokes.

if (!self.delegate || ![self.delegate respondsToSelector:@selector(downloadComplete:)]) {
  // delegate is gone, go away quietly
        [self autorelease];
        return;
    }
else {
  // delegate is still around
  [self.delegate downloadComplete:result];
}

I know I could,

a) have the downloader objects retain the view controller

b) keep an array of downloaders in the view controller, and set their delegate values to nil when I deallocate the view controller.

But I wonder if there is an easier way, where I just test if the delegate address contains a valid object?

Kenny Winker
  • 11,919
  • 7
  • 56
  • 78
  • If you could test if an address contained a valid object, by definition, it would, because it would be valid to access it to test? – Grant Paul Apr 21 '10 at 03:46
  • 1
    objective-c has a lot of abstractions... I can imagine a world where the runtime was able to tell the difference between an address with a valid object, and a deallocated one. – Kenny Winker Apr 21 '10 at 04:22

5 Answers5

30

I just ran into this problem and solved it. For ARC, the solution is to use the weak attribute instead of assign.

The crash come because the delegate

  1. Has an assign attribute, AND
  2. Has been deallocated.

The solution is to use the weak attribute, because when the object deallocates, the pointer WILL be set the nil . So when your code calls respondsToSelector on a nil, Objective C will ignore the call, and not crash.

In your code, when you attempt to call the respondsToSelector method on delegate, you get a EXC_BAD_ACCESS. This is because objects that use the assign property will not be set to nil when they are deallocated. (Hence why doing a !self.delegate before the respondsToSelector does not prevent the responseToSelector from being called on a deallocated object, and still crashes your code)

As already mentioned, using a strong or assign attribute on a delegate (as many have mentioned) in ARC will result in a retain cycle. So don't do it, you don't need to.

Drew H
  • 1,226
  • 1
  • 12
  • 13
  • besides using weak, is there other things required? Like eg, 'delegate = nil' in the main class or setting 'id _strong delegate'? I assume im haivng the same issue http://stackoverflow.com/questions/24466244/delegate-dont-exist-exc-bad-access/24970685#24970685 – Hexark Jul 26 '14 at 12:51
  • 2
    You should not need to do anything else. I read your question, and noticed that you provided a response indicating that using `weak` instead of `assign` fixed the crash. Are you still experiencing problems? – Drew H Jul 29 '14 at 04:59
  • Great explanation, however I am facing problem with weak delegate also. – ViruMax Feb 02 '15 at 07:34
  • Thank you, it's working fine! `if (self.delegate && [(NSObject*)self.delegate respondsToSelector:@selector(customMethod:clickedButton:)]) { [delegate customMethod:self clickedButton:[sender tag]]; }` with **weak** attribute delegete – Antony Raphel Dec 29 '16 at 08:36
10

No, you can't (usefully) "test if an address contains a valid object". Even if you were able to grub around inside the internals of the memory allocation system and determine that your address points to a valid object, that would not necessarily mean that it was the same object that you were previously referring to: the object could have been deallocated and another object created at the same memory address.

Retaining the delegate is the usual way to solve this. Your option (b) breaks object encapsulation, and might have thread-safety issues.

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • most of apple's implementations I can see don't retain the delegate... e.g. `@property(nonatomic,assign) id delegate;` am I mis-applying the design pattern? – Kenny Winker Apr 21 '10 at 04:15
  • That may be true for UI objects that require all interaction to happen on the main thread, but I think you'll find that implementations that are doing async operations in background threads (such as NSURLConnection) will retain their delegate. – David Gelhar Apr 21 '10 at 04:32
  • 6
    A **delegate** is NEVER retained! Only **targets** are retained. –  Apr 22 '10 at 11:37
  • 2
    Could you please explain more or provide a reference, @eaigner. I googled a bit and can find no definition of a __target__. – Kenny Winker Apr 23 '10 at 22:25
  • A delegate is an object that receives events, but is not retained by the sender. A target - for instance a NSButton/Control has a target and an action - is retained by the sender. –  Apr 28 '10 at 23:01
  • 4
    @eaigner What's your reference for the claim that a delegate is never retained? For example, the NSURLConnection method `-initWithRequest:delegate:` *does* retain the delegate (and is documented to do so). – David Gelhar Apr 29 '10 at 01:11
  • It's also not true that targets are retained all the time...UIGestureRecognizer addTarget:action: does not retain the target. – sqwerl Jan 05 '15 at 11:31
1

I also have problems with delegate weak reference, and currently I have only one solution for this problem: use strong reference for delegate, and manually set self.delegate = nil; after my code is complete. This solution works for me in async images loading, where you have some life cycle with visible ending.

Vitalii Gozhenko
  • 9,220
  • 2
  • 48
  • 66
1

I would just write

SEL slc = @selector(theSlc);
if ([delegate respondsToSelector:slc]) {
    [delegate performSelector:slc];
}

If the object is valid the method will be called, otherwise not. You do not have to check for

self.delegate != nil
  • 4
    You're probably right that my `!self.delegate` is redundant. But, I do know that `[delegate respondsToSelector:]` doesn't protect you from `EXC_BAD_ACCESS`. Try running this: NSObject *obj = [[NSObject alloc] init]; [obj release]; for (int i = 0; i<2; i++) { if ([obj respondsToSelector:@selector(retainCount)]) NSLog(@"retain count: %i",[obj retainCount]); } – Kenny Winker Apr 23 '10 at 22:46
1

I came across this question because my "downloader" object was giving me EXC_BAD_ACCESS. My solution was the cancel the downloader object right before I released it. Assuming your using NSURLConnection in your downloader object, call the cancel method on it.

Its important to note that if NSURLConnection is not currently downloading anything then calling cancel will result in a crash. You will need some logic to check if a download is in progress.

AlBeebe
  • 8,101
  • 3
  • 51
  • 65