Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 自定义NSCursor已损坏_C++_Objective C_Macos_Nscursor - Fatal编程技术网

C++ 自定义NSCursor已损坏

C++ 自定义NSCursor已损坏,c++,objective-c,macos,nscursor,C++,Objective C,Macos,Nscursor,我有以下代码从现有CGImage设置光标: NSPoint hotspot = ...; CGImageRef cgimg = ...; if(!cgimg) return nullptr; NSImage* nsimg = [[NSImage alloc] initWithCGImage:cgimg size:NSZeroSize]; if(!nsimg) return nullptr; NSCursor*

我有以下代码从现有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];
问题是它的渲染不一致,有时看起来正常,有时显示垃圾:

我使用的是完全相同的CGImageRef,它被渲染为正常图像(框下方的图像)

知道会有什么问题吗


同一应用程序中的库存游标呈现为OK。如果这很重要的话,那就在OSX“El Capitan”上。回答我自己的问题

似乎OSX使用了一些自动释放池之类的工具来存储中间结果

在我的实现中,我在一个地方创建光标,在另一个地方使用光标,以响应鼠标移动。在本例中,似乎OSX正试图使用已释放的内存

为了解决这个问题,我不得不使用
[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 ...

我认为你不应该打电话给[nsimg release]。默认情况下,ARC是启用的,不是吗?在该项目中,ARC是禁用的。