1

I am trying to figure out why my addObserver call in my app is causing an EXC_BAD_ACCESS when its selector should be called. Let me explain some more, I do my addObserver call in a method that invokes the MFMessageComposeView and then I add an observer for the "didFinish" notification in the 3rd party library, ShareKit. Now whenever the addObserver's selector should be called, a EXC_BAD_ACCESS is raised. Also my addObserver is in a NSObject class if that makes any difference.

This is the code I use for the addObserver:

        NSString *moredetailedshareText = [NSString stringWithFormat:@"Hey"];
        [SHKTextMessage shareText:moredetailedshareText];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SHKSharerFinished:) name:@"SHKFinishedCall" object:nil]; 

This is the postNotification call in ShareKit:

- (void)sharerFinishedSending:(SHKSharer *)sharer
{
    NSLog(@"finishedsending");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SHKFinishedCall" object:sharer];
    if (!sharer.quiet)
        [[SHKActivityIndicator currentIndicator] displayCompleted:SHKLocalizedString(@"Saved!")];
}

What would be the cause of this to not work?

Thanks!

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • Did you turn on [zombies](http://stackoverflow.com/a/4917557/1354100)? Possibly ShareKit is freeing something already released, are you using ARC? – Bejmax Jan 13 '13 at 19:48
  • No ShareKit is not using ARC. I haven't turned on zombies but I don't think that would help. – SimplyKiwi Jan 13 '13 at 20:20
  • I didn't ask if ShareKit uses ARC, i'm asking if you have ARC enabled in your build. NSZombiesEnabled will provide you more information of what exactly is being released when it should not be. – Bejmax Jan 13 '13 at 23:21
  • Oh, no I am not using ARC. Actually what I ended up doing what doing the NSNotification observer in another class and it worked for some odd reason however ShareKit is still giving me another problem that isn't related to NSNotifications. – SimplyKiwi Jan 13 '13 at 23:31

1 Answers1

3

If the crash occurs when the notification is posted, the solution is obviously a released target.

NSNotificationCenter does not retain registered observers. You have to make sure the observer is still alive while being registered and that it it unregistered before it's released.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • 1
    The crash occurs when the addObserver method should be called. So when the sendDidFinish notification should be called, thats where it crashes. – SimplyKiwi Jan 13 '13 at 20:19