notificationCenter should not be a member variable


#1

Hi jules,

It’s not a problem, but i have noticed something in JuceNSView class.
notificationCenter is a member variable while NSNotificationCenter is singleton and object is same for whole application.
so why not…

@interface JuceNSView : NSView
{
@public
NSViewComponentPeer* owner;
}

  • (JuceNSView*) initWithOwner: (NSViewComponentPeer*) owner_
    withFrame: (NSRect) frame
    {
    [super initWithFrame: frame];
    owner = owner_;

NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];

[notificationCenter  addObserver: self
                        selector: @selector (frameChanged:)
                            name: NSViewFrameDidChangeNotification
                          object: self];

if (! owner_->isSharedWindow)
{
    [notificationCenter  addObserver: self
                            selector: @selector (frameChanged:)
                                name: NSWindowDidMoveNotification
                              object: owner_->window];
}

[self registerForDraggedTypes: [self getSupportedDragTypes]];

return self;

}

  • (void) dealloc
    {
    [[NSNotificationCenter defaultCenter] removeObserver: self];
    [super dealloc];
    }

#2

It’s because of this dratted obj-c module cross-linking problem… If two modules contain the same named obj-c class and the wrong one gets invoked, it could end up using the singleton object from the wrong module… So doing it this way means that even if the wrong one is called, it should still be deregistering with the same pointer that it originally registered with. That’s the theory anyway…

Hopefully that should never happen if you’ve set up JUCE_ObjCExtraSuffix, but is there just in case.


#3

[quote=“jules”]It’s because of this dratted obj-c module cross-linking problem… If two modules contain the same named obj-c class and the wrong one gets invoked, it could end up using the singleton object from the wrong module.
So doing it this way means that even if the wrong one is called, it should still be deregistering with the same pointer that it originally registered with. That’s the theory anyway…

Hopefully that should never happen if you’ve set up JUCE_ObjCExtraSuffix, but is there just in case.[/quote]

Could this happen, if two of my modules have different namespace ?

Interestingly the documentation for NSNotificationCenter says.

Doesn’t it mean that my entire application is going to have a single notification-center object or it means every module in my code would have a single notification-center object. Am a little confused with it.


#4

obj-C doesn’t use namespaces.

I assume that “defaultCenter” is a static object, so would be local to each module in a process, but who knows…?


#5

Just an update on this topic

http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSDistributedNotificationCenter.html