I’m building a program with C++ by Mac Catalyst using Xcode to communicate with a composite hid device. I wrote a endless loop to test this function. Simply speaking , the process of my loop is as follows.
while(true){
1.Open HID interface with the composite HID device
2. If success, send “Get_FeatureReport” and get data from the device and print the result of the data
3. If success, close the interface and release the resource
4.Continue the loop
When I start the loop , I could always successfully get the correct data from the hid device , however , when it comes to 1020th iteration of the loop , it failed by Initialize failed with IOReturn = -536870201(0xE00002C7) by calling the following command.
kr = IOServiceOpen(usbInterface, mach_task_self(), pipRef, &connect);
And after that , it would always failed to open hid interface with the device unless I hot plug the device or restart the loop. During the failed case, system’s memory and CPU seems to be normal. I wonder that if might related with unreleased resource or maybe it’s because of the system limitation…I know that this is a critical case since user usually take such an action to communicate with the USB device , however , I still want to fix this issue in order to optimize my function’s code structure. Thank you for your patient for reading my question.
I tried to release all the resource such as io_iterator_t , usbInterface and etc.. but it always failed at 1020th iteration of the loop and after. Issue happened from the function of “IOReturn FindUSBInterface(IOUSBDeviceInterface245 **dev)”.
if (interfaceClass == kUSBHIDClass)
{
io_connect_t connect;
UInt8 direction;
UInt8 number;
UInt8 transferType;
UInt8 interval;
if(count==1020)
printf("1 0 2 0");
kr = IOServiceOpen(usbInterface, mach_task_self(), pipRef, &connect);
if (kr == kIOReturnSuccess)
{
kr = (*interface)->GetPipeProperties(interface, pipRef, &direction, &number, &transferType, &maxPacketSize, &interval);
if (kr != kIOReturnSuccess || transferType != kUSBControl)
{
(void) (*interface)->USBInterfaceClose(interface);
(void) (*interface)->Release(interface);
interface = NULL;
IOConnectRelease(connect);
continue;
}
usbDeviceInterface = interface;
IOObjectRelease(iterator);
return kIOReturnSuccess;
}
else
{
IOConnectRelease(connect);
IOObjectRelease(iterator);
(void) (*interface)->USBInterfaceClose(interface);
(void) (*interface)->Release(interface);
interface = NULL;
continue;
}
}
else
{
(void) (*interface)->USBInterfaceClose(interface);
(void) (*interface)->Release(interface);
interface = NULL;
continue;
}
1