views:

18

answers:

1

I'm writing a little test program where I can look at all the notifications being posted as various actions occur on an iOS devices. Unfortunately, I'm running into a problem where I get an EXC_BAD_ACCESS error when I even try to add an observer to the Darwin notification center. The relevant code is below:

void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {  
    NSNotification* n = [NSNotification notificationWithName:name object:object userInfo:userInfo];

    objc_msgSend(gSelf, sel_getUid(@"note:"), n);
    return;  
}  


- (void)viewDidLoad {
    [super viewDidLoad];
    gSelf = self;
    notifications = [[NSMutableArray alloc] initWithCapacity:10];
    self.title = @"Notification Log";

    CFNotificationCenterAddObserver(  
                                    CFNotificationCenterGetDarwinNotifyCenter(), //center  
                                    NULL, // observer  
                                    callback, // callback  
                                    NULL, // name  
                                    NULL, // object  
                                    CFNotificationSuspensionBehaviorHold  
                                    );   
}

-(void)note:(NSNotification *)notification{

    @synchronized(self){
        [notifications addObject:notification];
        [self.tableView reloadData];
    }
}
A: 

From CFNotificationCenter.h: (Look for the definition of CFNotificationCenterGetDarwinNotifyCenter.)

// - CFNotificationCenterAddObserver(): the 'name' argument may not be NULL (for this center).
Robot K
Thanks. I should add for anyone looking to listen to the majority of CFNotifications: http://blogs.oreilly.com/iphone/2008/08/iphone-notifications.html provides a way to get the strings needed to pass into the darwin center.
David Liu