views:

53

answers:

2

Hi all,

after having spent few months in trying to master the syntax and rules, I am going deeper in memory management rules. One thing I do not understand and causing me confusion is how one creates objects.

Based on what stated in apple memory management guide, the following is a valid approach:

– (NSArray *)sprockets {
    NSArray *array = [NSArray arrayWithObjects:mainSprocket,auxiliarySprocket, nil];
    return array;
}

because I am not causing any memory leaks. The reason why is that it's not using alloc for creating array and therefore sprockets is not the owner. However I am wondering now what's inside arrayWithObjects. Because it happens that in my apps I often have factory for creating custom objects using something similiar to:

return [[MyObject alloc] initWithParameter:(id)params]; // possible leak

If I want to change with a static method like:

return [MyObject initWithParameter:(id)params];

what could be in initWithParameter for adhere to memory rules ? And what if MyObject extends some other object ? I also find out that method naming rules are important to properly advise programmer, what are this rule ? Also could you point out a web link where this is explained (I am not yet good in finding docs on apple web site).

thanks

+1  A: 

This is the page you're looking for: Memory Management Rules. It all comes down to adhering to the rules of ownership for an object.

If you create an object using alloc then you own that object and must release it. For example:

NSString* str = [[NSString alloc] init]; 
//do something with your str object 
[str release];
str = nil; //don't forget to set it to nil, it's still 
           //pointing to the (now unallocated) block of memory

If you create an object using a factory method, for example:

NSString* str = [NSString stringWithString:@"blah"];

Then what is happening here is that it is creating the string object using alloc for you and then returning the object but first adding it to the local autorelease pool. To use the example in your question.

return [[MyObject alloc] initWithParameter:(id)params]; // possible leak
return [[[MyObject alloc] initWithParameter:(id)params] autorelease]; //now autoreleased -- no leak

Here is the page on Autorelease Pools. Apple say it better than me.

An autorelease pool is an instance of NSAutoreleasePool that “contains” other objects that have received an autorelease message; when the autorelease pool is deallocated it sends a release message to each of those objects. An object can be put into an autorelease pool several times, and receives a release message for each time it was put into the pool. Thus, sending autorelease instead of release to an object extends the lifetime of that object at least until the pool itself is released (the object may survive longer if it is retained in the interim).

Method naming rules are important as it gives a strong indication as to whether the method returns an autoreleased object. In this example I would name the method:

[MyObject myObjectWithParameter:(id)param]

Hope this has helped.

Greg Sexton
this helped a lot. Returning to the example in apple docs (sorry for the bad formatting), if I write NSArray *array = [[NSArray alloc] nitWithObjects:mainSprocket,auxiliarySprocket, nil]; [array autorelease]; can I achieve the same result as NSArray *array = [NSArray arrayWithObjects:mainSprocket,auxiliarySprocket, nil]; ?
Leonardo
Yes, if you "return [array autorelease];", this will achieve the same result. A point to note with arrays, in case you aren't aware, they send a retain message to every object added and a release to every object when you release the array. It is therefore not necessary to retain an object before adding to an array as this will increment the reference count twice.
Greg Sexton
another question: are there any advantages for a programmer with objective-c behaving this way ? I mean all the rules about owning and returning obejcts ? Can they be used in some (advanced) specific way or is it just a rule we have to stick to ?
Leonardo
I'm not 100% sure what you mean. I think it comes down to the fact that Objective-C is a superset of C. This means that when it comes to memory management, you can do what you like. You can release objects that have already been released just as easily as you can run off the end of the array. Having the rules and following them ensures that memory is properly disposed of cleanly and that your code works with, and makes sense to, others that may use it.
Greg Sexton
A: 

It is standard practice for alloc and creation methods to return a retained object. Anything that starts with init, create, or the object name will clue other programmers into the purpose of the method.

Even if you create with alloc, if the purpose of your method is to pass on ownership, you should not release the object.

Peter DeWeese
Not true. Anything you obtain by **alloc** or a method that starts with **new** or contains **copy** or that you have retained, you own. Nothing else.
JeremyP
If you were implementing one of those methods that starts with new or contains copy you would do exactly as I suggest if you want to program like Apple or any other major player.
Peter DeWeese