domesticcatsoftware/DCRoundSwitch — Issue #12: Crash on ln 363 of DCRoundSwitch.m, due to deallocated target
Author: jaredegan
Created: 2012-01-27 18:30:11+00:00
State: open

First of, thanks for such a great class!

There's a possibility of a crash when DCRoundSwitch calls `sendActionsForControlEvents`, if one of the targets of the switch as been deallocated. It's caused by delaying the `sendActions...` call to after the animations have completed. I assume that this is matching UISwitches behavior, but I don't think UISwitch has this crash. One way to reproduce it is to have a view controller be a DCRoundSwitch target, then flip the switch and hit back as soon as you can afterward, causing the view controller to be deallocated.

Exception:

*** -[SettingsController performSelector:withObject:withObject:]: message sent to deallocated instance 0x7a84020

Code:

[CATransaction setCompletionBlock:^{
[self removeLayerMask];
ignoreTap = NO;

// send the action here so it get's sent at the end of the animations
if (previousOn != on && !ignoreControlEvents)
[self sendActionsForControlEvents:UIControlEventValueChanged]; // (Crashes here)
}];

This can be fixed in the dealloc of the targets, by having the target remove itself from the switch, like so:

- (void)dealloc {
[self.mySwitch removeTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventAllEvents];
}

I'm not sure what the fix should be size I do believe the value changed event for UISwitches gets fired after the animation completes. At the least instructions to prevent this crash should be added to the docs.