Crash due to CALayer bounds contains NaN: [0 0; nan 20]

Discussion in 'iOS Programming' started by dbramhall, Sep 12, 2011.

  1. dbramhall macrumors newbie

    Aug 1, 2011
    England, UK
    I am working on an iOS app that incorporate tableviews within tab bars however when the user presses a tab bar item, the application crashes and I'm not sure why - the crash is due to CALayer bounds contains NaN: [0 0; nan 20] although I have no idea what to do because of this... Here's a link to the actual file:
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Break it down, then confirm what you expect.

    Two places in your code you have a complex expression like this:
    	topLabel =
    	[[[UILabel alloc]
    					 image.size.width + 2.0 * cell.indentationWidth,
    					 0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT),
    					 aTableView.bounds.size.width -
    					 image.size.width - 4.0 * cell.indentationWidth
    					 - indicatorImage.size.width,
    Break it down into separate observable steps, then add code that tells you the value at each step.

    In particulare, break out CGRectMake() so it assigns to a separate variable. Then use NSStringFromCGRect to convert to a string and log that string. If you see a NaN in the output, then you know which expression is producing the NaN.

    Then break it down more. Confirm that each expression for each of the 4 values in a CGRect is an actual number (i.e. not a NaN). To do that, break each one out into a separate variable and assignment, e.g.
    CGFloat top = calculation here;
    CGFloat left = calculation here;
    Then NSLog each of the 4 values. For the calculation that produces the NaN, confirm that each of the values involved is valid. Break it down again if you have to.
  3. dbramhall thread starter macrumors newbie

    Aug 1, 2011
    England, UK
    Thanks a lot but how does this change how the code performs - is there anything specifically wrong with the code?

    Thanks so much for your time, much appreciated!
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Impossible to tell. Too many unknowns.

    First, you only provided code, not a crash-report stack-trace, so no one knows exactly where it crashed. Without knowing where it crashed, there's no way to know which expression is producing the NaN.

    Do you know what NaN means? It means "Not a Number", so some calculation somewhere in your code is being performed with an invalid value, or is producing an invalid result. There can be any number of reasons for an invalid value or invalid result. That's why you have to narrow down the exact calculation that's producing the NaN.

    Second, you only provided a code fragment, not a complete compilable program that someone else can run. If no one can run it, no one can discover exactly where it crashes, so no one can find the specific point where the NaN is coming from.

    And frankly, you seem to be missing a #endif in the code you posted:
    		topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
    		bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
    	return cell;
    The last } shown above is the end of a method. If USE_CUSTOM_DRAWING is false, you have a big problem. If it's true, then you've probably made the entire rest of the file conditionally compiled. It may compile now, but it's wrong.
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    I think that the place these nan's usually come from is when a nil object returns a struct. You've got several places in that expression where you pull out a value from a member of a struct (bounds or size) that's returned from an object. Probably one of the objects is nil.
  6. Sykte macrumors regular

    Aug 26, 2010

    Joke... skip.

    So wait Storks bring babies and Nil's bring NaN's?


Share This Page