0

I'm using the following code many times in my app (especially to manage a NavigationController):

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 

When should I release it ?

Thx for helping,

Stephane

matm
  • 7,059
  • 5
  • 36
  • 50
Stephane
  • 4,978
  • 9
  • 51
  • 86

2 Answers2

7

Don't. Never release your application delegate - it is managed automatically by the OS.

If you look in your app's main.m file, you'll see some code that initializes an instance of UIApplication that represents your app - it is its responsibility to manage the application delegate's lifecycle, not your responsibility.

EDIT as @Goz points out, you should release it if at some point you retain it. However, since the application object (and therefore, by extension its delegate) is guaranteed to remain in scope for the life of the app (unless you go messing with it), it's far better to simply not do any memory management on the delegate, as this avoids the possibility of accidental over-release or other related issues.

Mac
  • 14,615
  • 9
  • 62
  • 80
  • 1
    unless of course you call retain on it ... then it needs to be released. – Goz Sep 10 '11 at 08:32
  • 1
    @Goz: true, but why would you go calling retain on it? It's guaranteed to be allocated for the life of the app (unless you go an do something silly like over release it, for example...) Simply avoiding retaining/releasing at all easily prevents accidental over-release. – Mac Sep 10 '11 at 08:36
  • Nice discussion +1 for each. :) – rptwsthi Sep 10 '11 at 08:43
  • 1
    I think I disagree. `UIApplication`, `UIAccelerometer`, and `NSFileManager` are singletons and playing with `release`/`retain` on it should not make sense if Apple implemented them in a strict way, so `retein` returns `self` and `release` does nothing. More on singletons here http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html – matm Sep 10 '11 at 09:25
  • Thx to you all for your thoughts! – Stephane Sep 10 '11 at 09:49
  • @delirus: we're discussing `UIApplicationDelegate`, not `UIApplication`. Otherwise, you're absolutely correct. – Mac Sep 10 '11 at 10:33
  • @Mac: you're right! I've allowed myself to change question title which is misleading and points to [UIApplication sharedApplication]. – matm Sep 10 '11 at 11:39
0

Short answer: never ever release your application delegate.

Explanation:
It often helps me how to address mem-mgmt issues, when I check how the things are declared. Take a look how delegate property is declared for UIApplication:

@property(nonatomic,assign) id<UIApplicationDelegate> delegate;

As you can see, it is assigned property meaning all mem-mgmt done here is just assigning pointers for an instance variable. It means calling release on your application delegate will result in -dealloc method being executed for your MyAppDelegate. Go and try this in debugger and you'll see that your application will receive EXC_BAD_ACCESS - i.e. it will crash.

EDIT: However, as Goz suggests, you can call retain and then release. But in the first place, it doesn't make sense to do this retain/release thing on app delegate.

matm
  • 7,059
  • 5
  • 36
  • 50