2

我有发布通知处理程序的代码

IONotificationPortRef notificationPortRef = IONotificationPortCreate(kIOMasterPortDefault);
if (!notificationPortRef)
{
    return nil;
}

CFRunLoopSourceRef notificationRunLoopSource = IONotificationPortGetRunLoopSource(notificationPortRef);
if (!notificationRunLoopSource)
{
    CFRelease(notificationPortRef);
    return nil;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), notificationRunLoopSource, kCFRunLoopDefaultMode);

CFDictionaryRef matching = IOServiceMatching (kIOUSBDeviceClassName);
if (!matching)
{
    CFRelease(notificationPortRef);
    return nil;
}

io_iterator_t matchingIterator;
kern_return_t result = IOServiceAddMatchingNotification(notificationPortRef,
                                                        kIOMatchedNotification,
                                                        matching,
                                                        &driverNotificationRoutine,
                                                        self,
                                                        &matchingIterator);

driverNotificationRoutine(self, matchingIterator);

if (result != KERN_SUCCESS)
{
    CFRelease(notificationPortRef);
}

CFRunLoopRun();

而这个回调处理程序

static void driverNotificationRoutine (void *refcon, io_iterator_t matchingIterator)
{
    NSLog(@"Callback called.");

    if (matchingIterator)
    {
            io_service_t device;
            NSInteger count = 0;
            while (device = IOIteratorNext(matchingIterator)) count++;
            IOObjectRelease(matchingIterator);

            if (count)
            {
                    ...
            }
    }
}

但我的回调函数永远不会被调用。我用 USB 闪存驱动器尝试了这个,但没有成功。我哪里错了?

4

1 回答 1

1

不应该有

IOObjectRelease(matchingIterator);

在回调函数中。

于 2011-07-05T05:18:20.013 回答