A crash on initWithWindowNibName?


#1

Hi,
I am going through this chapter, adding the same prefs panel to my own simple app.
On page 198 of the 4th edition, it states the following for within AppController.m:

- (IBAction)showPreferencePanel:(id)sender
{
    if (!preferenceController) {
        preferenceController = [[PreferenceController alloc] init];
    }
    NSLog(@"Showing %@.", preferenceController);
    [preferenceController showWindow:self];
};

I put a breakpoint at the preferenceController=… line, and it turns out that executing that line generates a Bad Access error. Given the very basic functionality on that line, I have no idea where the bad access comes from. And from what I read here, I am the first with this issue. I must be doing something wrong, somewhere!
I’d appreciate any pointers for solving this :confused:
Mark


#2

Well that was a quick fix. Sorry for bothering you all with this; to make sure no-one else is puzzled for too long about this, I will admit my mistake here :blush:

The init method that crashes calls code that can be found in PreferenceController.m, on page 203. For some obscure reason, I had this as the very first method:

- (id)initWithWindow
{
    self = [super initWithWindowNibName:@"Preferences"];
    return self;
}

This caused an infinite initialization loop, hence the bad access. The name of the method should be, of course, just init. I guess auto-complete got its way without me noticing…
Mark


#3

It wasn’t just you. That is the method stub that XCode puts into the new PreferenceController.m file…

So I really appreciate your solving it as I ran straight into it as well.


#4

thank you for posting this. I had the same issue.


#5

just a quick note, init is often used to init in a ‘default way’ i.e. in this case with the NIBName.

you would only create your own initWithNibName if:

  1. you have multiple nibs
  2. init: needs to remain as it is for some reason or another
  3. If you’re lazy and prefer the super doing the job :smiley: