views:

26

answers:

2

Hello, Still new to the memory management in iPhone apps, I have a newbee question. Let say I have some method that use attributes of the AppDelegate. At the beginning of those methods, I get the delegate like:

// Get delegate
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

Do I need to perform some kind of release at the end of the method (I was thinking of adding [delegate release] at the end of each methods like this one).

Generally speaking, do we have to release, at the end of the method, each object variables (NSString, NSData, ...) that were created within this method ?

Thanks a lot, Luc

+1  A: 

Simple rule:

If when you got a reference to an object (appDelegate in this case) you didn't call alloc on the object then you don't need release. If you did call alloc in that line of code you provided then you would want to call [delegate release];

jamone
Thanks a lot, that clarify a lot of things :)
Luc
It's not `init` that's the important part, it's `alloc`. The CARN rule, if a method contains Copy, Alloc, Retain or New, then you most likely need to release it.
Jasarien
Changed my answer from init to alloc. Thanks
jamone
+1  A: 

No. This is just a reference to your delegate which is most likely used by other parts of your program.

mcandre
ok thanks, btw, releasing an object when it is not necessary may cause any trouble ?
Luc
If you release your delegate, yes it will necessarily cause trouble. It's worth learning when to release and when not to. It's important to release only as many times as you need to, and not once more or less.
Dan Ray