Question: 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);

```

Answer: 
Generally, callbacks in Objective-C are handled by passing a delegate object and a selector to perform on that delegate. For example, this method will call a method on its delegate after logging a message, passing both itself and the message that was logged.

```
- (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];
    }
}

```

You might call it in code like this:

```
- (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);
}

```

You can also use `objc_msgSend()` directly instead, though you need to understand the Objective-C runtime enough to choose which variant to use and how to construct the prototype and function pointer through which to call it. (It's the mechanism by which message sends are actually implemented in Objective-C — what the compiler normally generates calls to in order to represent `[]` expressions.)
This is what Objective-C's selector is for:
[<http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/NSInvocationOperation_Class>](http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/NSInvocationOperation_Class/Reference/Reference.html)

The API isn't very intuitive, but its fine once you understand it

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