1

I am building a Mac application. I am adding a childWindowController to mainWindow. In my childWindowController, I have several buttons with their actions connected in IB. But when I press the NSButton, the application crashes and I get EXC_BAD_ACCESS message in the terminal. I also tried to perform setTarget:self, but that doesn't help at all.

Here's my code: applicationDidFinishLaunching

HomeWindowController *home_WindowController = [[[HomeWindowController alloc] initWithWindowNibName:@"HomeWindowController"] autorelease];<br/><br/> 
[[self window] addChildWindow:home_WindowController.window
                         ordered:NSWindowAbove];

And in the HomeWindowController:

- (id)initWithWindowNibName:(NSString *)windowNibName
{
     self = [super initWithWindowNibName:windowNibName];
    if (self) {
        // Initialization code here.

    }
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];
}

-(IBAction)action:(id)sender 
{
    NSLog(@"------------------ ");
}

What is wrong here? I am binding the NSButton to FileOwner and its action as well. Normally same as for iOS for IB. When I don't bind the IBAction, I don't get EXC_BAD_ACCESS.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Vacca
  • 1,219
  • 16
  • 26
  • Include the stack trace please. – Ramy Al Zuhouri Jan 17 '13 at 19:54
  • Whats that? If u mean terminal trace, I have exe bad access address error, when I click the NSButton Outlet. – Vacca Jan 17 '13 at 20:38
  • 1
    XCode version? ARC, and Garbage Collection, on or off? – Warren P Jan 17 '13 at 20:42
  • @WarrenP, I have turned off ARC and XCODE version is 4.4. The fact is when I bind NSButton on MainXib in AppDelegate it works fine. Also, when I don't bind the IBAction on FiLeOwner the app is not crashed. – Vacca Jan 17 '13 at 20:57
  • Have you tried [enabling zombie objects](http://stackoverflow.com/a/4917557/9530) to see if you're sending messages to deallocated objects? – Adam Rosenfield Jan 17 '13 at 20:57
  • @AdamRosenfield, I tried enabling zombies by which I get the error message : *** -[HomeWindowController performSelector:withObject:]: message sent to deallocated instance 0x100145800 . But nowhere in the code I am releasing the NSButton object. – Vacca Jan 18 '13 at 09:03
  • It finally got resolved, I was releasing the childWindowController on appDelegate method after adding it on main window.But I dont understand why cant I release it since it is a local instance of childWindowController on AppDelegate (just for adding it above)? – Vacca Jan 18 '13 at 09:18
  • Enabling zombie objects in Xcode is the hard way. [Use Instruments's Zombies template instead](http://stackoverflow.com/a/8863063/30461); then you can look over the history of the dead object and see what killed it. – Peter Hosey Jan 18 '13 at 17:41

1 Answers1

2

It finally got resolved, I was releasing the childWindowController on appDelegate method after adding it on main window.But I dont understand why cant I release it …

Because you own the window controller, and the window controller owns its window. The window isn't keeping its controller alive; you are. And when you're not, it dies out from under anything that might want to talk to it, such as a button that has it as its target.

More generally, trying to shrug off your ownership responsibilities onto other objects—e.g., expecting a window to own its WC for you—is asking for memory-management bugs.

(My only exception to that is indirectly owning objects through collections: if I own, say, an array full of Things, I don't retain and release each Thing individually on its way in and out. Anything else, I expect to outsmart me.)

since it is a local instance of childWindowController on AppDelegate (just for adding it above)?

I don't understand what you meant by that.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370