Crash in dragImageForSelection

John Brownie

I have what seems to be a simple operation to perform, but it crashes. I have a custom view which includes an NSTextView called textView. In the mouseDragged(with:) function in my view subclass, I have the following code (leaving out the logic deciding whether it is appropriate as a drag source):

            textView?.setSelectedRange(NSMakeRange(0, textView!.textStorage!.length))
            let dragImage = textView?.dragImageForSelection(with: event, origin: nil)

I've tried a variety of ways to select the whole text, but nothing changes the result. I get a crash in dragImageForSelection, with the message:

Cannot lock focus on image <NSImage 0x6000017f5640 Size={0, 0} Reps=() flipped:YES (flippedness is deprecated)>, because it is size zero.

The text is most often of length one (always in testing), and is often plain ASCII, but is always valid Unicode. Here's a sample of the textView and its textStorage:

(lldb) po textView
▿ Optional<NSTextView>
  - some : <NSTextView: 0x600003327200>
    Frame = {{2.50, 2.50}, {42.50, 42.50}}, Bounds = {{0.00, 0.00}, {42.50, 42.50}}
    Horizontally resizable: NO, Vertically resizable: NO
    MinSize = {34.00, 34.00}, MaxSize = {42.50, 10000000.00}

(lldb) po textView?.textStorage
▿ Optional<NSTextStorage>
  - some : 2{
    NSColor = "Generic Gray Gamma 2.2 Profile colorspace 1 1";
    NSFont = "\"LucidaGrande 22.50 pt. P [] (0x600000d719e0) fobj=0x100f6a270, spc=7.12\"";
    NSParagraphStyle = "Alignment 2, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 1.1, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n 140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n 308L,\n    336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation YES, HeaderLevel 0";

If I leave out the selection line, I don't get a crash, but then I don't get an image to drag.

Searching hasn't turned up anything of use. This was working in an earlier version of the app, written in Objective-C, and I'm unable to spot a significant difference in the code.

Anybody have an idea what is going wrong?

John Brownie
Mussau-Emira language, New Ireland Province, Papua New Guinea
Kouvola, Finland

Join to automatically receive all group messages.