1

Possible Duplicate:
Understanding reference counting with Cocoa and Objective-C

I'm little confused with memory leaking - when I have to release object, and when it will be released automatically in iOS, so please help me understood using following code. I have one method with following while block:

-(void) oneMethod {
    NSMutableArray *returnValue = [[[NSMutableArray alloc] init] autorelease];

    while(true) { 
    ...
     MyObject *myObj = [[MyObject alloc] initWithFrequency:100];

    [returnValue addObject:myObj];

    [myObj release];
    ...
    }
}
  1. Do I have to call [myObj release] or it will be released automatically in each loop?
  2. Also, do I have to put autorelease in NSMutableArray init call, or it will be automatically released immediately after I return from method?

Thank you!

Community
  • 1
  • 1
Alan 2012
  • 67
  • 5
  • Please see [Understanding reference counting with Cocoa and Objective-C](http://stackoverflow.com/questions/6578/understanding-reference-counting-with-cocoa-and-objective-c) – Matthias Bauch Dec 03 '12 at 14:50

3 Answers3

1

You should be using ARC - Automatic Reference Counting then you won't need to worry about releasing your allocated objects.

Adam Johnson
  • 2,198
  • 1
  • 17
  • 23
  • thank you Adam and Tom. So it's not like in C that if I exit some block {...code...} it will release objects in that block automatically. If I remember, ARC is introduced in new Xcode 4.x - What if I manually release objects and also use ARC - it will generate some sort of errors, or it is also allowed? – Alan 2012 Dec 03 '12 at 15:36
  • Correct if you try to `release` in a project using ARC, the compiler will complain. – Adam Johnson Dec 03 '12 at 15:39
  • so far as I know, release and retain are not valid methods in ARC enabled projects. – Dean Davids Dec 03 '12 at 15:40
1

Your code is correct.

I find that as you work more with it you will get used to it. I don't spend much time thinking about it.

Generally, you need to release everything (or autorelease) you make.

Your loop is coded correctly in that its easy on memory, even for large arrays.

Tom Andersen
  • 7,132
  • 3
  • 38
  • 55
1

The sooner, the better is what I learned. Your example is appropriately releasing each myObj variable once inserted into the array. The array now "owns" the object and the object will exist as long as the array exists, no reason for the variable reference to remain.

ARC does seem to make all this completely unnecessary. I wonder how many developers will even learn this concept in the near future as ARC does it all for you now.

Presumably, you are not using ARC and so, if you did not call [myObj release] it would result in a leak each time through the loop.

Dean Davids
  • 4,174
  • 2
  • 30
  • 44
  • I start to program on iOS several months ago using old xcode 4.0.2 reading (apparently) old books. Actually I didn't know, and I didn’t think of it that It could be huge difference between old and new xcode. So while reading “older” books, authors frightened me about memory leaks, that I have to be very careful with this, not to forget to release something.... And now I read that with ARC you don't have to think so much about it. I think It will take some time for me to be brave enough and to forget about releasing objects :-) Thank you, Alan – Alan 2012 Dec 03 '12 at 16:19