objective-c - 指针在 FSEventStream 回调中丢失(ObjC 和 ARC)

标签 objective-c callback automatic-ref-counting nsmutabledictionary

我已经成功地掌握了 FSEventStream 的基础知识,可以让我监视文件夹中的新文件事件。不幸的是,我传递给 FSEventStreamCreate() 的回调引用正在丢失/损坏/未保留,因此我也无法访问我需要的数据对象。以下是关键代码块:

FileWatcher.m :(设置 FSEvent 流)

FSEventStreamContext context;
//context.info = (__bridge_retained void *)(uploadQueue);  // this didn't help
context.info = CFBridgingRetain(uploadQueue);
context.version = 0;
context.retain = NULL;
context.release = NULL;
context.copyDescription = NULL;

/* Create the stream, passing in a callback */
stream = FSEventStreamCreate(NULL,
                             &FileWatcherCallback,
                             &context,
                             pathsToWatch,
                             kFSEventStreamEventIdSinceNow, /* Or a previous event ID */
                             latency,
                             kFSEventStreamCreateFlagFileEvents /* Also add kFSEventStreamCreateFlagIgnoreSelf if lots of recursive callbacks */
                             );

Filewatcher.m: FileWatcherCallback

void FileWatcherCallback(
                     ConstFSEventStreamRef streamRef,
                     FSEventStreamContext *clientCallBackInfo,
                     size_t numEvents,
                     void *eventPaths,
                     const FSEventStreamEventFlags eventFlags[],
                     const FSEventStreamEventId eventIds[])
{
    int i;
    char **paths = eventPaths;

    // Retrieve pointer to the download Queue!
    NSMutableDictionary *queue = (NSMutableDictionary *)CFBridgingRelease(clientCallBackInfo->info);
    // Try adding to the queue
    [queue setValue:@"ToDownload" forKey:@"donkeytest" ];
    ...
}

当触发此回调函数时,我可以正常获取文件路径,但指向 NSMutableDictionary 的 clientCallBackInfo->info 指针现在指向与我设置流时不同的内存地址。 然后,当我尝试添加到字典时,抛出了一个异常(setValue 行)。

我是否需要以某种方式以不同方式处理指针?任何帮助将非常感激。 (我在 Xcode 4.5.1 上使用默认build设置,包括 ARC。)

最佳答案

回调函数的第二个参数是 void *info(即 context.info),而不是指向 FSEventStreamContext 上下文的指针 结构。

因此这段代码应该可以获取正确的指针:

void FileWatcherCallback(
                         ConstFSEventStreamRef streamRef,
                         void *info, // <-- this is context.info
                         size_t numEvents,
                         void *eventPaths,
                         const FSEventStreamEventFlags eventFlags[],
                         const FSEventStreamEventId eventIds[])
{
    // ...
    // Retrieve pointer to the download queue:
    NSMutableDictionary *queue = CFBridgingRelease(info);
    // ...
}

备注:在我看来,您对CFBridgingRetain()/CFBridgingRelease()的使用还有另外一个问题。 uploadQueue 对象的保留计数将每次 回调函数被调用时递减。这会很快导致崩溃。

用起来可能更好

context.info = (__bridge void *)(uploadQueue);

用于创建事件流,以及

NSMutableDictionary *queue = (__bridge NSMutableDictionary *)info;

在回调函数中。只要使用事件流,您只需确保保持对 uploadQueue 的强引用。

关于objective-c - 指针在 FSEventStream 回调中丢失(ObjC 和 ARC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13628370/

相关文章:

objective-c - 显示用户位置的问题

ruby-on-rails - 所有模型的 ActiveRecord 全局回调

javascript - 将上下文添加到无上下文 Javascript 回调

iphone - ARC 在 iOS 4.3 中不工作

ios - 像 Instagram 一样带有圆角的文本背景

objective-c - 没有用于应用内购买的恢复按钮会导致拒绝

Javascript 回调 - 为什么这些回调以不同的顺序处理?

ios - UIViewController 没有自行解除分配

iOS:使用 IBOutlets 的 ARC 强引用或弱引用(用于自定义 UITableviewCell)

iphone - 在iOS中将GMT转换为IST