Re: NSAlert boxes ...

Peter Hudson

Thanks Sandor.

My use of  alert is very tightly scoped - and only used once - and not passed on.
Many thanks for the link.


On 16 Feb 2020, at 15:28, Sandor Szatmari <> wrote:


On Feb 16, 2020, at 08:18, Peter Hudson via Groups.Io <Peter.hudson@...> wrote:

Hi There

I see from the docs that when you create an NSAlert that you should dispose of it yourself.

You may have to dispose of it yourself or maybe ARC will take care of it for you.  If you’re writing MRR (not ARC) code you always have to -release/-autorelease objects you own (create).  

You take ownership of an object when you call -retain or -copy on an existing object, or use -new, -alloc/-init when creating an object.  As far as I’m aware there are no exceptions to these ownership rules.  In MRR you are responsible for calling -release/-autorelease on any object you used these methods to create/copy/take ownership.

These rules are essentially true in ARC too, except the compiler figures out when to call -release so you don’t explicitly call it.

Here is some additional reading on the topic.

My code for creating a simple alert and using it looks like this :-

NSAlert *alert = [[[NSAlert alloc] init] autorelease];
alert.messageText = @:Some message or other”;
[alert addButtonWithTitle:@"Create category"];
[alert addButtonWithTitle:@"Cancel"];
NSModalResponse choice = [alert runModal];

Etc …..

Is the use of an autorelease here O.K. ?

It appears to be but I cannot tell what else you do with ‘alert’.  -autorelease says that the object should live for the current scoped autorelease-pool.

So, if this alert panel is not supposed to live beyond the current autorelease-pool, then the code is fine.  If it does live beyond it, then you need to transfer ownership to that scope.


It certainly seems to work …..


Join { to automatically receive all group messages.