Chid
Chid

Reputation: 109

SIGSEGV crash while using delegate

I keep receiving this crash report and I can't figure out where the problem is. I understand that SIGSEGV exception type is usually due to a memory management issue.

Crash log,

zException Type:  SIGSEGV
Exception Codes: SEGV_ACCERR at 0x10
Crashed Thread:  0

Thread 0 Crashed:
0   libobjc.A.dylib                     0x394db0fc objc_retain + 12
1   Sparkle                             0x0012c9d7 -[PhotoAssetView didSelectButton:] (PhotoAssetView.m:44)
2   UIKit                               0x3193ada3 -[UIApplication sendAction:to:from:forEvent:] + 91
3   UIKit                               0x3193ad3f -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 39
4   UIKit                               0x3193ad13 -[UIControl sendAction:to:forEvent:] + 47
5   UIKit                               0x31926743 -[UIControl _sendActionsForEvents:withEvent:] + 375
6   UIKit                               0x3193a75b -[UIControl touchesEnded:withEvent:] + 595
7   UIKit                               0x318fe1a1 _UIGestureRecognizerUpdate + 5529
8   UIKit                               0x319359fd -[UIWindow _sendGesturesForEvent:] + 773
9   UIKit                               0x319353ab -[UIWindow sendEvent:] + 667
10  UIKit                               0x3190ad79 -[UIApplication sendEvent:] + 197
11  UIKit                               0x31909569 _UIApplicationHandleEventQueue + 7117
12  CoreFoundation                      0x2f14cf1f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
13  CoreFoundation                      0x2f14c3e7 __CFRunLoopDoSources0 + 207
14  CoreFoundation                      0x2f14abd7 __CFRunLoopRun + 631
15  CoreFoundation                      0x2f0b5471 CFRunLoopRunSpecific + 525
16  CoreFoundation                      0x2f0b5253 CFRunLoopRunInMode + 107
17  GraphicsServices                    0x33def2eb GSEventRunModal + 139
18  UIKit                               0x3196a845 UIApplicationMain + 1137
19  Sparkle                             0x0040a427 main (main.m:18)

Inside this method [PhotoAssetView didSelectButton:] we have calling one method using the unsafe_unretained delegate,

[self.delegate didSelectButtonForPhotoAsset:sender];

Any pointers?

Upvotes: 1

Views: 1949

Answers (2)

Vladimir
Vladimir

Reputation: 7801

Reference to delegate object must be of weak type: unsafe_unretained or _weak.

If PhotoAssetView delegate (probably it is a ViewController) is unsafe_unretained then when referenced object is deallocated, reference is no longer valid. At this address can be other object or garbage and sending message to the address leads to exception.

To fix:

  1. Use _weak references instead of unsafe_unretained. If reference is declared as _weak it is automatically set to nil when object deallocated (so be ready that it may became nil at random moment).
  2. If you for some reason you still want use unsafe_unretained reference you must manually set the reference to nil in dealloc of your PhotoAssetView delegate object:

//SomeViewController.m

- (void) dealloc{
  self.photoAssetView.delegate = nil;
}

Upvotes: 1

Wil Shipley
Wil Shipley

Reputation: 9533

Do you have a strong reference to the delegate elsewhere? Typically setting the delegate of an object doesn’t create a strong reference, so if you don’t keep one yourself it’s going to be freed before it ever gets called, and you’ll crash like this.

Upvotes: 3

Related Questions