Home > OS >  App crashes with EXC_BREAKPOINT on assigning border color of UICollectionViewCell
App crashes with EXC_BREAKPOINT on assigning border color of UICollectionViewCell

Time:11-01

I have code that flashes the border of a cell in a collection view by changing its color at set intervals. When the flash sequence begins, the original color of the cell is saved, so that when the flash sequence is complete, the color can be restored. The crash that occurs doesn't explain exactly what is wrong, so I'm left trying to deduce the problem, and what I'm seeing in the debugger isn't making much sense.

Note that this is code that in the past has been tested to work without crashing, not sure why it's suddenly failing now.

The error: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1831e1fd4) (The line appears in red and execution will not continue, so no this is not just stopping at a breakpoint, it is a crash.)

The code...

@interface OrderCollectionViewCell : UICollectionViewCell 

               .   .   .

@property (strong, nonatomic) NSTimer *borderTimer;
@property (nonatomic) BOOL blinkStatus;
@property (nonatomic) NSInteger blinkTimes;

               .   .   .

@implementation OrderCollectionViewCell
{
    CGColorRef normalBorder;
}

-(void)flashBorder
{
    // Save original color for later restore
    normalBorder = self.bg.layer.borderColor;
    
    if(self.borderTimer) {
        [self.borderTimer invalidate];
    }
    self.borderTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(blink) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:self.borderTimer forMode:NSRunLoopCommonModes];
    
    self.blinkTimes = 0;
    self.blinkStatus = NO;
}


-(void)blink
{
    if (self.blinkTimes   == 3)
    {
        // Sequence complete.  Restore original color
        self.bg.layer.borderColor = normalBorder;  // <-- This line crashes
        [self.borderTimer invalidate];
        self.borderTimer = nil;
        return;
    }
    
    if(self.blinkStatus == NO){
        self.bg.layer.borderColor = [UIColor whiteColor].CGColor;
        self.blinkStatus = YES;
    }else {
        self.bg.layer.borderColor = [UIColor orangeColor].CGColor;
        self.blinkStatus = NO;
    }
}

Here's where things get weird. According to the enter image description here

Note that the address of each is reported as <CGColor 0x281b6d200>, which is more like object behavior than struct behavior.

When the time comes to restore the color, the variable normalBorder no longer appears to contain valid color data, as it's simply reported as an address (the same address as before) but no descriptive output:

enter image description here

Does anyone know what might be causing this failure, or why in the debugger the CGColorRef seems to be acting like an object rather than a struct?

CodePudding user response:

As you have guessed, "normalColor" is no longer a valid reference by the time you access it. I think there is either an error in the docs or a miscommunication as CGColorRef is actually derived from CFTypeRef and not a struct. I recommend you save the color in a property as a UIColor object. This will solve your issue.

  • Related