我在 Objective-C 类中有一个方法。它有 2 个用 C 编写的回调函数。类指针,即self
被传递给这些函数作为void *
。在 C 函数中,我创建一个类型为 class 的指针并分配void *
范围。
第一个回调函数执行成功。但是void *
指针变为nil
在第二个回调函数中。请注意,我没有在第一个回调中调整指针,但我仍然得到nil
在第二次回调中。
有什么想法可能会出问题吗?
例如:
kr = IOServiceAddMatchingNotification(gNotifyPort, kIOFirstMatchNotification,
matchingDict, RawDeviceAdded, NULL,
&gRawAddedIter);
RawDeviceAdded(NULL, gRawAddedIter, self);
这很好用。但下面的函数接收self
as nil
.
kr = IOServiceAddMatchingNotification(gNotifyPort, kIOFirstMatchNotification,
matchingDict, BulkTestDeviceAdded, NULL,
&gBulkTestAddedIter);
BulkTestDeviceAdded(NULL, gBulkTestAddedIter, self);