Callback handling

I have a method in the objective-C class. It has 2 callback functions written in C. The pointer to the ie class selfis passed to these functions as void *. In C functions, I create a type class pointer and assign a parameter void *. The first callback function succeeds. But the pointer void *becomes nilin the second callback function. Note that I did not change the pointer in the first callback, but still I get nilin the second callback.

Any ideas what could be wrong?

For example:

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

RawDeviceAdded(NULL, gRawAddedIter, self);

It works great. But below the function gets selflike nil.

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

BulkTestDeviceAdded(NULL, gBulkTestAddedIter, self);
+5
source share
3

IOKit? , , , IOServiceMatchingCallback 2 , 3. RawDeviceAdded() BulkTestDeviceAdded(), IOServiceMatchingCallback self (refCon), 3-. , - IOServiceAddMatchingNotification(), .

C Objective-C , . , :

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
+10

, Objective-C . , , , , .

- (void)logMessage:(NSString *)message
          delegate:(id)delegate
    didLogSelector:(SEL)didLogSelector
{
    NSLog(@"%@", message);

    if (delegate && didLogSelector && [delegate respondsToSelector:didLogSelector]) {
        (void) [delegate performSelector:didLogSelector
                              withObject:self
                              withObject:message];
    }
}

:

- (void)sayHello
{
    [logger logMessage:@"Hello, world"
              delegate:self
        didLogSelector:@selector(messageLogger:didLogMessage:)];
}

- (void)messageLogger:(id)logger
        didLogMessage:(NSString *)message
{
    NSLog(@"Message logger %@ logged message '%@'", logger, message);
}

objc_msgSend() , , Objective-C , , , . ( Objective-C - , , [].)

+1

, Objective-C : http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/NSInvocationOperation_Class

The API is not very intuitive, but it is beautiful as soon as you understand it.

You may also need to do refactoring, now there may be a better way, but when I had this problem, my solution was to refactor and use InvoationOperation.

0
source

All Articles