CALayer position contains NaN: [nan nan] During Drag and Drop

Can't seem to track this one down. My app creates a dragging session. During drag and drop, if I drag around the screen for awhile I sometimes hit this crash.


CALayer position contains NaN: [nan nan]

2016-01-14 18:08:00.753 MY APP NAME [23373:356523] (

0 CoreFoundation 0x00007fff9b5e8ae2 __exceptionPreprocess + 178

1 libobjc.A.dylib 0x00007fff9791e73c objc_exception_throw + 48

2 CoreFoundation 0x00007fff9b5e898d +[NSException raise:format:] + 205

3 QuartzCore 0x00007fff99f5914e _ZN2CA5Layer12set_positionERKNS_4Vec2IdEEb + 152

4 QuartzCore 0x00007fff99f590ae -[CALayer setPosition:] + 44

5 HIToolbox 0x00007fff8cc5e711 _ZL20UpdateLayerPositionsP14OpaqueCoreDrag + 630

6 HIToolbox 0x00007fff8ce830f8 _Z29UnflockAnimationTimerCallbackP16__CFRunLoopTimerPv + 759

7 CoreFoundation 0x00007fff9b52ebc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20

8 CoreFoundation 0x00007fff9b52e853 __CFRunLoopDoTimer + 1075

9 CoreFoundation 0x00007fff9b5ace6a __CFRunLoopDoTimers + 298

10 CoreFoundation 0x00007fff9b4e9cd1 __CFRunLoopRun + 1841

11 CoreFoundation 0x00007fff9b4e9338 CFRunLoopRunSpecific + 296

12 CoreFoundation 0x00007fff9b570db5 CFMessagePortSendRequest + 949

13 HIServices 0x00007fff867017a8 SendDragIPCMessage + 530

14 HIServices 0x00007fff866febdd CoreDragMessageHandler + 1321

15 CoreFoundation 0x00007fff9b571238 __CFMessagePortPerform + 584

16 CoreFoundation 0x00007fff9b4f8839 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41

17 CoreFoundation 0x00007fff9b4f87a9 __CFRunLoopDoSource1 + 473

18 CoreFoundation 0x00007fff9b4e9e1b __CFRunLoopRun + 2171

19 CoreFoundation 0x00007fff9b4e9338 CFRunLoopRunSpecific + 296

20 HIToolbox 0x00007fff8cc1f935 RunCurrentEventLoopInMode + 235

21 HIToolbox 0x00007fff8cc1f76f ReceiveNextEventCommon + 432

22 HIToolbox 0x00007fff8cc1f5af _BlockUntilNextEventMatchingListInModeWithFilter + 71

23 AppKit 0x00007fff8bf930ee _DPSNextEvent + 1067

24 AppKit 0x00007fff8c35f943 -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454

25 AppKit 0x00007fff8c1a4f9c NSCoreDragCGEventBlockingProc + 135

26 HIServices 0x00007fff866f867e SampleMouseAndKeyboard + 141

27 HIServices 0x00007fff866f8374 DragInApplication + 50

28 HIServices 0x00007fff866f7403 CoreDragStartDragging + 705

29 AppKit 0x00007fff8c1a2171 -[NSCoreDragManager _dragUntilMouseUp:accepted:] + 1010

30 AppKit 0x00007fff8c47fbbe _handleBeginDraggingSession + 97

31 CoreFoundation 0x00007fff9b4f7e37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23

32 CoreFoundation 0x00007fff9b4f7da7 __CFRunLoopDoObservers + 391

33 CoreFoundation 0x00007fff9b4e9a3a __CFRunLoopRun + 1178

34 CoreFoundation 0x00007fff9b4e9338 CFRunLoopRunSpecific + 296

35 HIToolbox 0x00007fff8cc1f935 RunCurrentEventLoopInMode + 235

36 HIToolbox 0x00007fff8cc1f76f ReceiveNextEventCommon + 432

37 HIToolbox 0x00007fff8cc1f5af _BlockUntilNextEventMatchingListInModeWithFilter + 71

38 AppKit 0x00007fff8bf930ee _DPSNextEvent + 1067

39 AppKit 0x00007fff8c35f943 -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454

40 AppKit 0x00007fff8bf88fc8 -[NSApplication run] + 682

41 AppKit 0x00007fff8bf0b520 NSApplicationMain + 1176

42 MY APP NAME 0x0000000100093602 main + 34

43 libdyld.dylib 0x00007fff99f425ad start + 1


Any ideas as to what can cause it? I don't see anything helpful in the debugger. Adding an exception breakpoint doesn't show me anything helpful either.

I've never actually used multi-image dragging, so I can't help you there, but go ahead and give it a try with the image components provider. After all, it just might work. Even if it doesn't work, you haven't really lost anything.

Thanks for your help. I'm going to give it a shot.


I guess the CALayer issue happens sometimes when you attempt to hide a dragging item by either setting contents to nil, or returning an empty array in image components provider block, or setting the image components provider block to nil. The documentation doesn't seem very clear on this though.... the comments in the header seem to indicate that you should be able to hide drag items. Not sure if the CALayer thing will be considered a bug by Apple....

Somewhere under the covers it seems they are trying to position an item with 0 size and are diving by 0, instead of just retuning out if a dragging item's frame has NSZeroSize. But that of course is a guess. I hope the issue is tied to this hunch, otherwise all drag/drop operations potentially could run into it, which would not be a good look. Perhaps I'll try to reproduce this with a simple demo app, and just set the image components provider block to nil and see if I can reproduce the issue when I have more time. Documentation says that you can set the block to nil.


Far as optimizing, my hope is that creating the icon within the block will be better than using setDraggingFrame:contents:...and that the system will be reasonable about only calling it when necesssary. I don't think there is much of a point in creatiing 300 images for a drag when they are going to stack on top of eachother. It turns out that I did need to optimize my code for generating dragging images a bit, so I also keep a cache for drag images my app generates. I noticed performance problems when dragging lots of items at once. I don't have time right now to test how often the provider block gets called, doesn't seem like there is much I can do about it anyway. While I was trying to optimize my dragging image code I noticed the CALayer issue. Pretty sure the bug was always there, but since it is intermittent I must have missed it before.


Edit: It seems the documentation confirms my hunch about optimizing many drag images by using a provider block instead of setDraggingFrame:contents:. The answer to that question was in front of my face...guess I was coding for too many hours yesterday and missed it.


Below is the documentation for setDraggingFrame:contents:

Figured I'd paste it here. May help someone else out in the future:


Alternate single image component setter.

This method simplifies modifying the components of an

NSDraggingItem
when there is only one component.

This is a convenience method. This method sets the draggingFrame and creates a single NSDraggingImageComponent instance with one image corresponding to the NSDraggingImageComponentIconKey key. You should only use this method under the following conditions: the drag image for this item is composed of a single image., or there are a reasonable number of dragging item instances being created or enumerated.

If your application requires the dragging of hundreds of items this method would create a instance for each item when it is called. Compare this to the imageComponentsProvider block which is much faster to define and allows the Application Kit to create only a subset of the items using imageComponentsProvider.

Also notice that I get this when dragging NSCollectionViewItems. Select all items (including some offscreen, and NSCollectionView won't generate a drag image for those)...and move the mouse around a bit and that nan bug will blow up your app and you will cry like I am crying right now. 😢

Did you ever figure this one out? I've noticed this NaN crash when doing exactly what you say here: Dragging items that have been scrolled off screen.


It's not consistent though, and I'm not dragging many items. Had the crash occur with just 5.

I ended up subclassing NSCollectionView and overriding all the dragging source and dragging destination methods. Not fun, but that's all I was able to come up with.

On 10.12.4...I just tried commenting out my workaround code to see if they fixed it...my workarond code (override the colllection view drag source stuff)..which is ugly and *slow* because I have to enumerate offscreen items..and throw an image for items that really don't need to be seen (since they should stack because there's so many of them)... but the only way to ensure you don't a nAn exception thrown is to do this.


With my workaround code commented out, the issue is still there. It's been over a year. Rushing to file bug reports, while it helps Apple find bugs they deem important, doesn't seem to do much for developers 95% of the time. Not only is it volunteer work, you usually don't get the fix you want either.

On 10.13.2, I just hit this same issue.

CALayer position contains NaN: [nan nan] During Drag and Drop
 
 
Q