Re: NSAlert boxes ...

Graham Cox

On 17 Feb 2020, at 4:27 pm, Jon Gotow <gotow@...> wrote:

If you were to use [NSAlert beginSheetModalForWindow:completionHandler:], then you'd have to worry about lifecycle of the alert extending beyond the current function context.

That is the case I’m talking about and no, it works fine even if you don’t “worry” about the lifecycle.

Just alloc + init + autorelease, then beginSheet…. it works, it has always worked since completion blocks were introduced.

It also works for the -runModal case also, you can autorelease prior to that call and that also doesn’t cause a lifecycle issue.

You can certainly “worry” about extending the lifecycle into the completion block if you want, except the unbalanced release will throw a compiler warning. That in itself shows how unnecessary it is to bother with.

TL;DR: Normal rules apply, stop overthinking this! Just because an NSAlert has some unusual asynchronous lifespan doesn’t mean it needs to be treated any differently from any other object you instantiate.


Join to automatically receive all group messages.