0

I want to pass an object in view A to view B, that's work, but when I repeat this method, I have a crash (Thread 1: EXC_BREAKPOINT).

I initialize in my view B as :

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hotSpotMore:) name:@"HotSpotTouched" object:nil];
    }
    return self;
}


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

        self.articleVC = [[ArticlesVC alloc] init];
        self.articleVC=[notification.userInfo objectForKey:@"Art"]; // ERROR LINE


    }

In my View A as :

        NSDictionary *myDictionary=[NSDictionary dictionaryWithObject:articles forKey:@"Art"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"HotSpotTouched" object:self userInfo:myDictionary];

I recover my object in instance variable, for the first two time, that's work and after there are a crash.

Output: ArticlesVC setArticleVC:]: message sent to deallocated instance 0x44883f10

And in my instrument Zombie I have this eror : An Objective-C message was sent to a deallocated 'ArticlesVC' object (zombie) at address: 0xc2d0710. 

My issue is method dealloc is called twice and I have a Zombie because my "RefCt" is set to "-1", I don't understand why this method is called twice time. How i can solve that ?

Pierre_S
  • 73
  • 3
  • 13

2 Answers2

1

Your viewB is already dealloced, but viewA send object to viewB, which already doesn't exist. Add removeObserver into dealloc:

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
  • I forgot to put this part of my code, I did that and this method are called twice, there are my issue... I don't know why – Pierre_S Feb 14 '14 at 11:49
  • @Pierre_S, `dealloc` is called twice? How many times `init` is called? – Valentin Shamardin Feb 14 '14 at 11:53
  • When it's works init and dealloc are called one time (First time) and when it doesn't work there are one init and two dealloc (Three time). Between when that work and doesn't work there are two init and two dealloc, maybe the issue comes from here. (Second time) – Pierre_S Feb 14 '14 at 14:19
0

Your notification observer will be added everytime you call initWithNibName for your class. Try removing the earlier observer before adding a new one.

you can do this in either in

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

or

- (void)viewdidUnload 
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
Dinesh
  • 929
  • 7
  • 25