English 中文(简体)
Why does NSNotificationCenter throw an exception when I release it?
原标题:

This isn t so much a question as a pondering thought - why does NSNotificationCenter throw an exception when it s released? I m still new to iPhone development, and thus don t know the innards of Cocoa yet, so it d be good to understand why.

I m assigning the defaultCenter to a variable, calling addObserver:selector:name:object and then releasing the previous variable, but the call to [notify release] crashes the app. I m not doing anything strange in the code, so it d be interesting to find out exactly why it s doing this.

Anyone ran into this problem?

最佳回答

From what I know of NSNotifcation you shouldn t be assigning the defaultcenter to a variable but rather doing something like:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showLogin) name:@"IncorrectLogin" object:nil];

In that snippet your calling the default centre and registering the current object for a certain message.

And then to post a message to the notification centre you can use:

[[NSNotificationCenter defaultCenter] postNotificationName:@"IncorrectLogin" object:nil];

I use the above in all my code and don t have any problems with it.

问题回答

There should only be one default notification center for your app, so none of your classes should be retaining or releasing it. You wouldn t want your notification center to disappear on you, right?

I don t think you own the object, and therefore should not release it.

Remember the NARC: New Alloc, Retain, Copy. If you do one of these, you have to release it.

It s not yours to release.

Remember the NARC rule--you ONLY release things that you brought into existence using:

New Allocate Retain, or Copy.

NARC. See?

What you re doing with NSNotificationCenter is you re getting a copy of the singleton that represents the default notification center. It d be worth reading up on singletons.





相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签