Search code examples
runtime-errornswindowsetbackground

Window setBackGround: causes runtime errors


I added a background color to the window of a well-running simple draw project and build succeeded. Then the fun began: runtime errors appeared in the console window. I found no help in Apple docs and Google. When the same happened in another draw project, I knew I had to ask for help.

Here's what happened each time I did a build & debug and stop -- runs 1) through 4). "continue" means I clicked the Continue icon. It kept on erring in no consistent order.

1)Program received signal:“EXC_BAD_ACCESS". spinning ball. paths OK.

continue. “EXC_BAD_ACCESS. spinning ball. paths disappear. background appears.

2)continue, 6 times. “EXC_BAD_ACCESS”. spinning ball. paths disappear. background appears.

3)immediately:

2012-12-26 09:53:18.265 bezier triangle[388:a0f] -[NSCFArray stroke]: unrecognized selector sent to instance 0x1005184f0  

2012-12-26 09:53:18.268 bezier triangle[388:a0f] -[NSCFArray stroke]: unrecognized selector sent to instance 0x1005184f0
2012-12-26 09:53:37.846 bezier triangle[388:a0f] -[NSCFArray stroke]: unrecognized selector sent to instance 0x1005184f0
2012-12-26 09:53:37.847 bezier triangle[388:a0f] -[NSCFArray stroke]: unrecognized selector sent to instance 0x1005184f0

no continue icon. paths OK. no background.

4)continue, 6 times. “EXC_BAD_ACCESS”. spinning ball. no paths. no background.

The NSWindow class creates a couple of Bezier paths in initWithRect: and draws them in drawRect:. It ran well. I got the setBackGround: code from Apple's Window Programming Guide and pasted it into the previously empty AppDelegate.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Background color
    [window setOpaque:NO]; // YES by default 
    NSColor *semiTransparentBlue =
    [NSColor colorWithDeviceRed:0.0 green:0.0 blue:1.0 alpha:0.5]; 
    [window setBackgroundColor:semiTransparentBlue];    
    window.backgroundColor = NSColor.blueColor;
}

Setting breakpoints showed that the runtime errors occur after this code finishes.

Thinking that the problems might have something to do with the timing of its execution, I moved this code to initWithRect and then into awakeFromNib. These didn't work. I wish I could find some example that shows how to implement this. All help will be appreciated.


Solution

  • No, setBackground: did not cause the runtime errors.

    In the paths code, the paths weren't being allocated and inited properly, although the project ran correctly until the setBackground: code was added. Strange!

    I'm sorry I didn't enclose the paths code in my question because it was too long.

    I found my mistakes by starting a new project and building it up slowly, a piece at a time.