4

I've added a NSNotificationCenter observer that calls 2 selectors with the same name on two different view controllers.

It works, but when I run the app sometimes it crashes with this error message:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)

or

Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

Image

Anyone has an idea why it crashes? Thank you!

.

My code:

fetchFromParse:

-(void)sendAllStores
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"getStoresArrays" object:nil userInfo:self.storesDict];
}

firstVC.m:

- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getStoresArrays:) name:@"getStoresArrays" object:nil];
}

-(void)getStoresArrays:(NSNotification*)notification
{
    NSLog(@“Working”);  
}

secondVC.m:

-(void)prepareArrays
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getStoresArrays:) name:@"getStoresArrays" object:nil];
}
-(void)getStoresArrays:(NSNotification*)notification
{
    NSLog(@“Working”);
}

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.secVC=[[secondVC alloc] init];
    [self.secVC prepareArrays];

    fetchFromParse *fetchFromParseObj=[[fetchFromParse alloc] init];
    [fetchFromParseObj getStoresFromParse];

    Return YES;
}
FS.O
  • 403
  • 6
  • 24

4 Answers4

3

Notification Crashes

Typically you might see a crash of this nature if you attempt to close a view controller without removing the observer. So that part of the answer provided by Mr. Patil, is absolutely required. Your use-case will vary about where and how you remove the observer. If you remove it too soon you might end up crashing if you try to reference any related objects.

So you might want to remove the self.storesDict object or at least verify that it is not nil before using it.

Debugging

Here's a quick tutorial on debugging with Xcode. It's for an older version but it should get you up to speed quickly. You can also review Apple docs for more on collecting crash logs.

Logs

You should go to the reports navigator and copy a bit more of your log so that we can determine a more precise cause of the issue. You can add a breakpoint right before the faulty code and then step through the issue. Copy the debug console if nothing else.

Getting the crash log

You can open Xcode go the Window menu select Devices. Select your attached device (iPhone/iPad) and click the View Device Logs button. From the list select your app name/crash date. Copy the data to the question.

More Information

The more information you provide on crashes the more likely we can help you. I suspect that the answer is you are either trying to access something that is nil or not releasing the observer at the right time. It might not be appropriate to release the observer when your view disappears but you have not provided enough information to make that obvious.

How do the different view controllers work together? Are you certain that the Notifications is causing the crash?? Put a breakpoint at the post notification and in each selector and then debug the app until it crashes. You will need to identify the conditions that precede the crash. I'll refine this answer if you let me know when you update the question.

Tommie C.
  • 12,895
  • 5
  • 82
  • 100
1

First thing I would do is do a global search and make sure no other classes are listening to that notification. Then I would make sure that in the classes that you have added an observer for that notification you do the following:

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
ascendancy05
  • 227
  • 2
  • 16
0

Add this

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    //Has to be unregistered always, otherwise nav controllers down the line will call this method
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

hope it helps.

Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46
0

Are you sure that 'addObserver', 'postNotification' and 'removeObserver' are always called on the main thread?

Please doublecheck with:

if ([NSThread isMainThread])
OlDor
  • 1,460
  • 12
  • 18