zombie event in Cocoa
Asked Answered
T

1

0

I have a EXC_BAD_ACCESS error. I used Profile in xCode 4 to see what is happening with memory and saw that it is zombie event:

An Objective-C message was sent to a deallocated object(zombie) at address ... 

I found mentioned address in Object List. Responsible caller is

-[NSWindowTemplate nibInstantiate]

Zombie Responsible Caller - [NSApplication(NSWindowCache) _checkForTerminateAfterLastWindowClosed: saveWindows:]

Before zombie event there're -[NSWindow _close], [NSWindow retain] and several [NSWindow release] events (Ref count = 0 after last [NSWindow release])

EXC_BAD_ACCESS occurs when I close application. I commented all code so nothing is executing after launching of application. I launch it and immediately close - and error fires.

Why _checkForTerminateAfterLastWindowClosed launch, how to prevent EXC_BAD_ACCESS?Any ideas?

Thanks

Timothytimour answered 4/12, 2011 at 18:55 Comment(0)
C
3

We can safely assume _checkForTerminateAfterLastWindowClosed is called to check whether the application should terminate after its last window is closed. We can further assume that it does this by asking your application delegate.

I just noticed in your previous question that your window is your application's delegate. So I'm guessing that by:

EXC_BAD_ACCESS occurs when I close application.

you meant that it happens when you close the window. (An application can't be closed; that verb doesn't go with that noun. You close windows and quit, or terminate, applications.)

When you close the window (assuming that it was the only window you had up), the application goes to ask its delegate whether it should quit as a result of that. Unfortunately, the object you set as its delegate is the window that you just closed, and thereby killed off.

So that's the dead object that is being messaged (Instruments will confirm this): Your window, which is also your application's delegate.

The best solution is to split the object in two (or three): Make a direct subclass of NSObject, and an instance of that to be your application's delegate, and have that create and own a window controller, which loads the window.

Carlenecarleton answered 4/12, 2011 at 20:47 Comment(4)
I reviewed Object Summary in Instruments. NSApplication object was alive when zombie message occured. Information about zombie is presented in my post: An Objective-C message was sent ... - when I click "Done" - Object Summary opens where last records are [NSWindow _close], [NSWindow retain], [NSWindow release] and then [NSApplication(NSWindowCache) _checkForTerminateAfterLastWindowClosed: saveWindows:]. I updated a little bit my initial postTimothytimour
@IlyaBlokh: I know the application was alive; it sent the message. Please re-read my answer.Carlenecarleton
I got the problem. So about solution: what object you suggest to split?Why subclass of NSObject, not of NSWindow?(I inherited main class from NSWindow, as you can see in my previous question link ).Timothytimour
@IlyaBlokh: A window should not be the application's delegate, because windows come and go during the application's lifetime. Split your window-that-is-also-the-application's-delegate into two objects, one that is a plain object and is the application's delegate, and the other that is your window. I would also put a window controller in between, owned by the application's delegate and owning the window.Carlenecarleton

© 2022 - 2024 — McMap. All rights reserved.