views:

203

answers:

1

I'm not sure if I'm doing something wrong here:

I'm registering for Workspace notifications using this snippet in awakeFromNib

[[[NSWorkspace sharedWorkspace] notificationCenter] 
    addObserver:self 
       selector:@selector(noteReceived:) 
           name:nil 
         object:nil];

the selector noteReceived: takes a single NSNotification * as a parameter. And I've got a breakpoint on it.

When compiled with GC turned off it works fine, and I receive all notifications.

When complied with GC turned on, I only get one notification when my app launches, and that's it.

Am I missing something?

Solution:

I was missing something. This was just a quick test project so there wasn't the usual connection between controllers that there would be in a real app. It isn't enough to instantiate an object in a nib/xib file and expect it not to be collected.

Once I made my controller a delegate of File's owner (even though it doesn't implement any delegate methods) that was enough to keep the object alive.

+2  A: 

Under GC, NSNotificationCenter only maintains a weak reference to your observing object. Because of that, make sure that your observing object is rooted somewhere in your object hierarchy.

Ashley Clark
Thanks, it helped me find a solution.
Abizern