0

I have the following code to set cursor from existing CGImage:

    NSPoint   hotspot = ...;
    CGImageRef cgimg = ...;

    if(!cgimg)
        return nullptr;
    NSImage* nsimg = [[NSImage alloc] initWithCGImage:cgimg size:NSZeroSize];
    if(!nsimg)
        return nullptr;

    NSCursor* pcur = [[NSCursor alloc] initWithImage: nsimg hotSpot: hotspot];
    if(!pcur)
        return nullptr;

    [nsimg release];

    // later in the code I set it as current:
    [pcur set];

Problem is that it renders inconsistently, sometimes it looks OK, sometimes it shows garbage: broken cursor

I am using exactly the same CGImageRef that is rendered OK as normal image (that one below the box).

Any idea what problem could be?

Stock cursors in the same application are rendered OK. That is on OS X "El Capitan" if that matters.

4

1 に答える 1

0

私自身の質問に答えます。

OS X は、自動解放プールなどを使用して中間結果を保存しているようです。

私の実装では、マウスの動きに応じて、ある場所でカーソルを作成し、別の場所で使用しています。この場合、OS X は解放されたメモリを使用しようとしているようです。

[pcur push]; [pcur pop];OS Xにカーソルをすぐに作成させるためにシーケンスを使用する必要があったことを修正するために:

NSPoint   hotspot = ...;
CGImageRef cgimg = ...;

if(!cgimg)
    return nullptr;
NSImage* nsimg = [[NSImage alloc] initWithCGImage:cgimg size:NSZeroSize];
if(!nsimg)
    return nullptr;

NSCursor* pcur = [[NSCursor alloc] initWithImage: nsimg hotSpot: hotspot];
if(!pcur)
    return nullptr;

[nsimg release];

[pcur push]; [pcur pop]; // this is mandatory to force cursor structures to be created.

// store pcur for later use ...
于 2015-10-21T23:22:18.910 に答える