0

I saw the following piece of code

   UIAlertView *alert = 
   [[UIAlertView alloc] initWithTitle :@”Hello World!” 
                                   message:@”iPhone, here I come!”  
                                  delegate :self
                         cancelButtonTitle:@”OK” 
                         otherButtonTitles:nil ];
   [alert show];
   [alert release];

Do I need to call release every time, or is there a way to automatically manage memory like garbage collection in java or .NET?

JasonFruit
  • 7,283
  • 4
  • 42
  • 60
user705414
  • 17,800
  • 36
  • 105
  • 151

3 Answers3

3

You can use ARC (Automatic Reference Counting), it is not garbage collection but kind of compiler functionality to let developers not worrying about memory management.

Check out this question and this link

In brief ARC is just like Manual reference counting (MRC) with the compiler figuring out when to call retain/release.

You can also convert any non -ARC project to use ARC from XCode (Edit->Refactor->Convert to Objective-C ARC). You can also selectively set projects/files to not use ARC (e.g. third party code).

Community
  • 1
  • 1
msk
  • 8,635
  • 5
  • 39
  • 70
  • 2
    It is perfectly fine to "pass allocated objects and release across boundaries" with ARC and non-ARC code. That's one of the big selling points of ARC. You do have to conform to the memory-management conventions regarding method names to make sure the ARC and non-ARC code agree on how to handle things. – Ken Thomases Jul 07 '12 at 14:11
  • Thanks Ken, I edited my answer. I have no idea what was on my mind while making that comment (probably my C++ exp). But thank you very much for pointing it out. 1 up – msk Jul 07 '12 at 15:05
1

Java and .NET are platforms that are garbage collected and they run in their own virtual environments. Objective-C is built on top of C and the memory management is handled by the developer. Apple introduced something called Automatic Reference Counting (ARC) which takes care of releasing objects that have no pointers to them, but in any case ARC is not like garbage collection. Therefore you need to read rules for memory management as well as /or how to use ARC correctly...

graver
  • 14,963
  • 4
  • 44
  • 62
  • 1
    +1 You can add that ARC is available starting iOS 5 and that the use of retain / release is still needed for low level programming. – moxy Jul 07 '12 at 11:51
0

The other answers are certainly correct. In case you do go for manual memory management: You only need to release objects that you get by messages whose name starts with "alloc", "copy", "new" or "mutableCopy". All other objects returned from messages are not owned by you and don't need to be released. Most of them are added to an auto-releasepool. You should also follow that convention and always return objects that are autoreleased (regarding the exceptions listed above).

Edit: Oh and one more thing: Because of this autorelease- behavior you need to retain objects you want to keep by either assigning them to a retained property of your class or doing it manually with [ retain]. Following and remembering these three rules lets you get by nicely.

Michel Müller
  • 4,733
  • 3
  • 23
  • 47
  • First, you forgot "mutableCopy". Second, things which you get from methods not named that way are not necessarily autoreleased. It's just that you don't own them. They may or may not be autoreleased. The point is, it's not your concern. Something else is responsible for the memory management. You are right, of course, that if you want something to stick around, you have to make sure you own it. If it didn't come from a method that gives you ownership, you have to retain it. – Ken Thomases Jul 07 '12 at 14:15