Search code examples
iphoneuiviewios6exc-bad-accesslayoutsubviews

Custom UIView causes a EXC_BAD_ACCESS code = 1 in layoutSubviews


I have created a custom UIView which is instantiated 3 times from UIViewController. From the viewDidLoad method of that UIViewController:

self.remainingDays = [[RemainingTileView alloc] initWithFrame:CGRectMake(20, 49, 80, 75)];
self.remainingHours = [[RemainingTileView alloc] initWithFrame:CGRectMake(120, 49, 80, 75)];
self.remainingMinutes = [[RemainingTileView alloc] initWithFrame:CGRectMake(220, 49, 80, 75)];

[self.view addSubview:self.remainingDays];
[self.view addSubview:self.remainingHours];
[self.view addSubview:self.remainingMinutes];

In the RemainingTileView class, I have this layoutSubviews method:

- (void)layoutSubviews {
    [super layoutSubviews];

    if (self.number)    // This is an NSNumber property
        self.numberLabel = [self labelForNumber:[self.number intValue]];
    else
        self.numberLabel = [self labelForNumber:0];

    if (self.unit)    // This is an NSString property
        self.unitLabel = [self labelForUnit:self.unit];
    else
        self.unitLabel = [self labelForUnit:@""];

    [self configView];
}

When creating the view, it crashes on the line if (self.number) with the stack frame:

* thread #1: tid = 0x2403, 0x39f6c526 libobjc.A.dylib`objc_retain + 6, stop reason = EXC_BAD_ACCESS (code=1, address=0x10000010)
frame #0: 0x39f6c526 libobjc.A.dylib`objc_retain + 6
frame #1: 0x000dc742 myProject`-[RemainingTileView layoutSubviews](self=0x1e892b80, _cmd=0x344cde51) + 106 at RemainingTileView.m:63
frame #2: 0x3405d802 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 258
frame #3: 0x33e07d8a QuartzCore`-[CALayer layoutSublayers] + 214
frame #4: 0x33e07928 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 460
frame #5: 0x33e0885c QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
frame #6: 0x33e08242 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 238
frame #7: 0x33e08050 QuartzCore`CA::Transaction::commit() + 316
frame #8: 0x33e07eb0 QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 60
frame #9: 0x322276cc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
frame #10: 0x322259c0 CoreFoundation`__CFRunLoopDoObservers + 276
frame #11: 0x32225d16 CoreFoundation`__CFRunLoopRun + 742
frame #12: 0x32198ebc CoreFoundation`CFRunLoopRunSpecific + 356
frame #13: 0x32198d48 CoreFoundation`CFRunLoopRunInMode + 104
frame #14: 0x35d6f2ea GraphicsServices`GSEventRunModal + 74
frame #15: 0x340ae300 UIKit`UIApplicationMain + 1120
frame #16: 0x000d3448 Project Countdown`main(argc=1, argv=0x2fd2ecf8) + 116 at main.m:17

self.number is an instance of NSNumber. The UI is being modified from the main thread. I have looked for existings solutions here on stackoverflow but nothing worked.

What am I missing? What should I look for?


Solution

  • Looks like your properties have incorrect declarations. Probably assign or weak instead of retain, copy, or strong.