Objective c IONotificationPortDestroy-打电话还是不打?
我有代码可以在OS X 10.7.4上使用XCode 4.3.3设置USB设备添加/删除通知。对于带有myVid和myPid的USB设备,这是一个很好的样板:Objective c IONotificationPortDestroy-打电话还是不打?,objective-c,macos,usb,iokit,Objective C,Macos,Usb,Iokit,我有代码可以在OS X 10.7.4上使用XCode 4.3.3设置USB设备添加/删除通知。对于带有myVid和myPid的USB设备,这是一个很好的样板: // Global declarations somewhere near the top of the file. IONotificationPortRef g_notificationPort= NULL; io_object_t g_notification= 0; io_iterator_t g_iteratorAd
// Global declarations somewhere near the top of the file.
IONotificationPortRef g_notificationPort= NULL;
io_object_t g_notification= 0;
io_iterator_t g_iteratorAdded= 0;
.
.
.
- (BOOL)setupDeviceNotification
{
// Set up matching dictionary.
NSMutableDictionary* matchingDictionary= (NSMutableDictionary*)IOServiceMatching(kIOUSBDeviceClassName);
[matchingDictionary setObject:[NSNumber numberWithLong:myVid] forKey:[NSString stringWithUTF8String:kUSBVendorID]];
[matchingDictionary setObject:[NSNumber numberWithLong:myPid] forKey:[NSString stringWithUTF8String:kUSBProductID]];
// Create a run loop source for the notification object.
g_notificationPort= IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopSourceRef notificationRunLoopSource= IONotificationPortGetRunLoopSource(g_notificationPort);
CFRunLoopAddSource(CFRunLoopGetCurrent(), notificationRunLoopSource, kCFRunLoopDefaultMode);
// Set up a notification callback for device addition on first match.
kern_return_t kRet= IOServiceAddMatchingNotification(g_notificationPort, kIOFirstMatchNotification, (CFMutableDictionaryRef)matchingDictionary, deviceAddedCallback, (void*)self, &g_iteratorAdded);
// Rudimentary error handling.
if(KERN_SUCCESS != kRet)
{
[matchingDictionary release];
return FALSE;
}
// Arm the notification and check for existing devices.
[self deviceWasAdded:g_iteratorAdded];
return TRUE;
}
这段代码运行良好,当添加设备时,我使用IoniotificationPortRef使用IOServiceAddInterestNotification并将设置在全局对象中的io_对象存储起来
在分析这段代码进行一些重构(将全局变量转换为类中的对象变量)后,我意识到我从来没有在我的IoniotificationPortRef对象上调用IoniotificationPortDestroy。我应该叫它吗?另外,我没有对IOServiceAddInterestNotification中分配的io对象做任何事情-那里需要清理吗?好的,在翻阅了大量苹果文档后,我发现我应该对io对象执行IOObjectRelease,这就是通知。我找到了一些参考资料,但最简单的是苹果开发者网站上的“用户模式USB设备仲裁”文档