How to detect where NaN is passing to CoreGraphics

2019-03-09 12:35发布

问题:

I have very large graphic Mac app and now I receive a lot of the following messages in Console on 10.9 GM.

<Error>: Error: this application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API. This is a serious error and contributes to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

I noticed that these messages appear in debugger after calling [NSApp nextEventMatchingMask: untilDate inMode: dequeue] but I think the reasons are in some other places. But I have too many places where I use Cocoa Graphics. I didn't receive this kind of message before 10.9.

How to detect where NaN is passing to CoreGraphics API?

回答1:

After much digging around, I've found you can set a symbolic breakpoint on "CGPostError" in Xcode, and that will give you a stack trace.



回答2:

I was getting this error when I was foolishly retaining an NSPopover for future use.

It seems popover.showRelativeToRect(_:) is all you need to do, and then you can forget about it.



回答3:

I found the problem. It's dividing by zero at some point that leads to NSAffineTransform with NaN elements in the matrix. For some reasons compiler and OS passed this situation before 10.9.



回答4:

so you should get an exception at that point, so an exception breakpoint should work...

here are things to look out for...

you may have messed up a method signature... ie

-(float)myHeight
{
    return 56.0;
}

gets messed up in a subclass

-(int)myHeight
{
    return 42;
}

or you have some bad math(s) that emit a NaN...

there are a couple of ways to detect a NaN... c99 introduced isnan()
also the ieee float trick of (f != f) will be true for NaN