开发者

Getting EXC_BAD_ACCESS, can't figure out how to fix it

开发者 https://www.devze.com 2023-04-02 04:09 出处:网络
Currently I\'m learning Obj-C for Mac developing, with Cocoa. I made a simple file browser with an inspector, to see a file\'s icon an some info. So far, so good. Now I made it document based, so I co

Currently I'm learning Obj-C for Mac developing, with Cocoa. I made a simple file browser with an inspector, to see a file's icon an some info. So far, so good. Now I made it document based, so I could have more than one open windows.

To tell the inspector which file it should inspect, I use the NSWindowDidBecomeMainNotification. Works fine for switching between windows, but it gives an EXC_BAD_ACCESS when I close all windows and then open a new one.

This is the method that handles the notification:

- (void)windowChanged: (NSNotification *)notification
{
    NSWindow *window = [notification object];
    BrowserWindow *doc = [[window windowController] document];

    if (currentDocument != doc) {

        [currentDocument.arrayController removeObserver: self
                                             forKeyPath: @"selectionIndex"];

        [icon setImage:nil];
        [size setStringValue:@"-"];
        [owner setStringValue:@"-"];
        [filename setStringValue:@"(none selected)"];

        [doc.arrayController addObserver: self
                              forKeyPath: @"selectionIndex"
                                 options: NSKeyValueObservingOptionNew 
                                 context: NULL];
        currentDocument = doc;
    }
}

The error occurs where it calls removeObserver:forkeyPath: on the currentDocument.arrayController. It kinda makes sense, it tries to remove the observer for something that doesn't exist anymore, 'cause the window is closed. But how to fix it? I just can't think of anything else..

Could someone point me in the right directions?

I appreciate the help! :)

--

It's getting weirder.. Just checked the example that was downloadable from the website of the book I've got, and they're using the same approach, but it w开发者_Go百科orks all fine. Can't find any differences, it's driving me crazy.

--

Solved! More details later.


Daniel is probably right: You probably don't retain currentDocument. Make currentDocument a property:

@property (retain) BrowserWindow *currentDocument;

And synthesize it in the implementation section:

@synthesize currentDocument;

And change your code to:

- (void) windowChanged: (NSNotification *) notification
{
    NSWindow *window = [notification object];
    BrowserWindow *doc = [[window windowController] document];

    if (self.currentDocument != doc) 
    {
        [self.currentDocument.arrayController removeObserver: self
                                                  forKeyPath: @"selectionIndex"];

        [icon setImage: nil];
        [size setStringValue: @"-"];
        [owner setStringValue: @"-"];
        [filename setStringValue: @"(none selected)"];

        [doc.arrayController addObserver: self
                              forKeyPath: @"selectionIndex"
                                 options: NSKeyValueObservingOptionNew 
                                 context: NULL];
        self.currentDocument = doc;
    }
}

You might want to do the same for icon, size, owner and filename.

And heed the warning of the message: you probably don't register self as observer to start with.


To tell the inspector which file it should inspect, I use the NSWindowDidBecomeMainNotification. Works fine for switching between windows, but it gives an EXC_BAD_ACCESS when I close all windows and then open a new one.

This is part of the problem right there. When the last window closes, no window will become main. So, you also need to handle the case where a window resigns main, as happens when it closes (and when another window becomes main).

Your inspector probably should both retain the document and switch documents after a delay, using a timer (whose fire date you postpone every time another did become/resign main notification comes in) or delayed perform (which you cancel and re-perform every time). When the timer/perform fires, find out what document, if any, is the active document, and update the inspector accordingly.

Also note that you can have no active document (no document window is the main window) even when there are documents open. The About panel and your Preferences panel are two good ways to achieve (and test) this.

0

精彩评论

暂无评论...
验证码 换一张
取 消