处理回调 [英] Handling Callbacks

查看:159
本文介绍了处理回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法在一个objective-C类。它有2个用C语言编写的回调函数。类指针 self 作为 void * 传递给这些函数。在C函数中,我创建一个类型类型的指针,并分配 void * 参数。
第一个回调函数成功执行。但在第二个回调函数中 void * 指针变为 nil 。注意,我没有在第一个回调中调整指针,但仍然在第二个回调中获得 nil





例如:

  = IOServiceAddMatchingNotification(gNotifyPort,kIOFirstMatchNotification,
matchingDict,RawDeviceAdded,NULL,
& gRawAddedIter);

RawDeviceAdded(NULL,gRawAddedIter,self);

这很好。但下面的函数接收 self nil

  kr = IOServiceAddMatchingNotification(gNotifyPort,kIOFirstMatchNotification,
matchingDict,BulkTestDeviceAdded,NULL,
& gBulkTestAddedIter);

BulkTestDeviceAdded(NULL,gBulkTestAddedIter,self);你是IOKit回调例程中的特殊问题吗?/ b>

你给出的具体例子的问题是IOServiceMatchingCallback只需要2个参数,而不是3.你需要你的RawDeviceAdded()和BulkTestDeviceAdded()回调函数来匹配IOServiceMatchingCallback原型,并接受self作为第一个参数(refCon),而不是第三。此外,您需要传入自我作为IOServiceAddMatchingNotification()的第二个到最后一个参数,以通过回调将它传回给您。



一种常见的方法在Objective-C代码中处理C回调只是要有一个静态函数将回调转发给你的实例。因此,您的示例回调代码如下所示:

  static RawDeviceAdded(void * refcon,io_iterator_t iterator)
{
[(MyClass *)refcon rawDeviceAdded:iterator];
}

@implementation MyClass
- (void)setupCallbacks
{
// ...所有前面的设置已剪切
kr = IOServiceAddMatchingNotification (gNotifyPort,kIOFirstMatchNotification,matchingDict,RawDeviceAdded,(void *)self,& gRawAddedIter);
//调用回调方法一次'arm'迭代器
[self rawDeviceAdded:gRawAddedIterator];
}
- (void)rawDeviceAdded:(io_iterator_t)iterator
{
//在这里照顾迭代器,确保完成迭代重新设置
}
@end


I have a method in an objective-C class. It has 2 callback functions written in C. The class pointer i.e. self is passed to these functions as void *. In the C functions I create a pointer of type class and assign the void * parameter. The first callback function executes successfully. But the void * pointer becomes nil in the 2nd callback function. Note that I haven't tweaked pointer in the first callback but still I get nil in 2nd callback.

Any ideas what might be going wrong?

For example:

kr = IOServiceAddMatchingNotification(gNotifyPort, kIOFirstMatchNotification,
                                      matchingDict, RawDeviceAdded, NULL,
                                      &gRawAddedIter);

RawDeviceAdded(NULL, gRawAddedIter, self);

This works fine. But below function receives self as nil.

kr = IOServiceAddMatchingNotification(gNotifyPort, kIOFirstMatchNotification,
                                      matchingDict, BulkTestDeviceAdded, NULL,
                                      &gBulkTestAddedIter);

BulkTestDeviceAdded(NULL, gBulkTestAddedIter, self);

解决方案

Are your problems specifically with the IOKit callback routines? The problem with the specific example you gave is that the IOServiceMatchingCallback takes only 2 parameters, not 3. You need your RawDeviceAdded() and BulkTestDeviceAdded() callback functions to match the IOServiceMatchingCallback prototype and to accept self as the first parameter (refCon), not the 3rd. Also, you need to pass in self as the second-to-last parameter of IOServiceAddMatchingNotification() to get it passed back to you by the callback.

A common method for handling C callbacks in Objective-C code is just to have a static function that forwards the callback to your instance. So, your example callback code would look like this:

static RawDeviceAdded(void* refcon, io_iterator_t iterator)
{
    [(MyClass*)refcon rawDeviceAdded:iterator];
}

@implementation MyClass
- (void)setupCallbacks
{
    // ... all preceding setup snipped
    kr = IOServiceAddMatchingNotification(gNotifyPort,kIOFirstMatchNotification, matchingDict,RawDeviceAdded,(void*)self,&gRawAddedIter );
    // call the callback method once to 'arm' the iterator
    [self rawDeviceAdded:gRawAddedIterator];
}
- (void)rawDeviceAdded:(io_iterator_t)iterator
{
    // take care of the iterator here, making sure to complete iteration to re-arm it
}
@end

这篇关于处理回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆