Do I need to autorelease out parameters of my function ? - objective-c

I have a method that accepts three NSMutableArrays, one as Input and two as output using pointer to a pointer.
Now I am creating the two arrays inside the method.
So the memory allocation is done inside the method therefore it should be method's responsibility to release the memory.
So should I call autorelease on the objects before assigning them to these output references ?

You can autorelease them. But make sure that, after the method call, you call retain on both the output pointers. Release them once you are done with them
NSMutableArrays *arr1, arr2, arr3;
[self methodOne:arr1 two:*arr2 three:*arr3];
[arr2 retain];
[arr3 retain];

If you allocate or retain an object, you need to release or autorelease it. It's as simple as that.

Related

When would you need to call alloc when creating a new object

I understand the whole business around reference counting and "owning an object" and that if you allocate an object in Objective-c, it's your responsibility to release it
However when exactly would you need to call alloc on a newly created object? Would it only be to retain the reference after the end of the scope or is there some other reason
You need to call alloc in order to allocate the memory for the object.
The typical setup of an object is something like:
Object *obj = [[Object alloc] init];
The alloc call allocates memory for the object, and the init call initialises it (gives it sensible default values for all attributes/properties).
Some object types come with factory methods, eg
NSArray *arr = [NSArray array];
In this case, the object is initialised and allocated by the single array call.
None of this has anything (directly) to do with reference counting, except that different ownership rules normally apply to the two methods.
I think you are misunderstanding a basic concept. sending alloc to a class will result in creating a new object of that class (not initialized yet) which you own (retain count will be 1).
from your question "when exactly would you need to call alloc on a newly created object?" -
if the object is newly created it means that someone already allocated it..
if you meant: when do you need to call retain on a newly created object? the answer is if you want to hold it yourself and not rely on whomever allocated it, and might release it sometime.. remember that alloc/new syntax raises the retain count by one, where as other creating methods (like [NSArray array]) return autorelease objects..
in general i would recommend using ARC and not be bothered by these issues..
I'm not exactly a objective-c guy, but I don't think you call alloc on any object, you call it on a class to allocate the object and call init on the newly allocated object.
You may want to retain to retain the reference after the release is performed by autorelease pool, if this is your setup. That often happens to the object created using [NSThing thingWithStuff:stuff] or some such.

difference between – initWithRequest:delegate: and +connectionWithRequest:delegate

I want to know the difference between –initWithRequest:delegate: and +connectionWithRequest:delegate: of NSURLConnection?
Just the first one is no-aotorelease object and second is autorelease?
I want to know which one should I use in my ios code?
You've already pointed out the difference
For iOS codes, it's best to use use Alloc/init rather than convenience auto release functions because they stay around after you are done using them and there is no way for you to release them
Use autorelease when it's not possible to know when to call release (such as when you are returning a object that is not being retained elsewhere) The closest autorelease pool will free the memory the next time the pool is drained.
If you do use alloc/init, remember to call release, otherwise it will linger on in the memory
All methods in Objective-C that starts with init require to be called only after alloc method. Also init-method returns non-autoreleased object.
All methods that starts from name similar to class name, for example, [NSString string], [NSArray array] and others returns autoreleased objects and don't require precalled alloc method.
Returning to you question: you can use any of that approaches: alloc + initWithRequest:delegate: or connectionWithRequest:delegate: but be sure to release object in first case.
I think you pointed out the only difference.
I use the +connectionWithRequest:delegate: method
I think something internal is retaining the connection until it fails or finishes

Reference on when to release in Objective-C

I'm having a recurring problem in Objective-C. I'm either releasing things too many time, or not enough. or perhaps I'm not retaining them enough...
Can someone point me at a good reference that will give me a rule of thumb on when I need to retain and release?
For example:
I remember reading somewhere that some objects come pre-retained, so I need to release them, but not retain them. Which objects are these?
if I alloc an object and only need it in that method, do I need to release it? retain it?
Obviously, if I retained something, I needtorelease it, but beyond that, I get a bit lost.
The rules are generally pretty simple. If you get an object in one of the following ways:
id obj = [[MyObject alloc] init];
id obj = [myObject retain];
id obj = [myObject copy];
id obj = [myObject mutableCopy];
then you need to release it at some point -- in the same method, or your dealloc method, generally. In other words, balance your calls to alloc, retain, copy, and mutableCopy with a matching release call.
I remember reading somewhere that some objects come pre-retained, so I need to release them, but not retain them. Which objects are these?
This happens rarely. The documentation for the called method should specify that you are responsible for releasing the returned object; otherwise, you should assume you're receiving an autoreleased object.
if I alloc an object and only need it in that method, do I need to release it? retain it?
Yes, you need to release it (but you don't need to retain it). (You can also use one of the convenience methods that return an autoreleased object if you're only going to use it in that method.)
There is one and only one canonical reference: Apple's Memory Management Guide for Cocoa or iPhone.

Do removeAllObjects and release of an NSMutableArray both have the same functionality?

I have written the following line of code:
NSMutableArray *array=[[NSMutableArray alloc]init];
This allocates some memory. My question is, how can we later release this memory, whether using removeAllObjects method or [array release]?
Do both methods have the same functionality?
When you add an object to the array, the object's retain count will increase by 1. When you remove that object from the array, retain count will decrease by 1 to balance it out. But if you release the array, all objects will automatically receive a release message. So you don't need to call removeAllObjects before releasing the array.
Technically, these two method are not same. If you call removeAllObjects, the array will become empty and all objects will receive a release message but the array itself is still not released. The array will be released when you call release on it.

Objective-C Memory Management: When do I [release]?

I am still new to this Memory Management stuff (Garbage Collector took care of everything in Java), but as far as I understand if you allocate memory for an object then you have to release that memory back to the computer as soon as you are finished with your object.
myObject = [Object alloc];
and
[myObject release];
Right now I just have 3 parts in my Objective-C .m file: #Interface, #Implementation and main. I released my object at the end of the program next to these guys:
[pool drain];
return 0;
But what if this program were to be a lot more complicated, would it be okay to release myObject at the end of the program?
I guess a better question would be when do I release an object's allocated memory? How do I know where to place [myObject release];?
This is probably a little over-simplified, but in general, you are going to want to release it where you declared it.
If you declare an object INSIDE a particular method call, then by definition, you will be done with that object (or at least that handle to that object) at the end of that method call... release it then.
If you declare an object as an instance variable, then by definition you will be done with it when that instance is destroyed... release it in the dealloc method of that class.
Keep in mind that "release" does not equal "destroy." When passing objects around in your application, it may make sense to have more than one handle to that object stored in different places... in that case "release" means "I'm done with this object, but someone else may still be using it." Deallocation only occurs when the number of "handles" (retain count) reaches zero.
Apple has some fantastic documentation on memory management, I would check it out at developer.apple.com.
You essentially have three kinds of objects, each with a different pattern.
Transients Objects
In general, you should autorelease transient objects. These are objects that are allocated locally and do not need to exist beyond the method in which they are called. Or they are passed around from method to method.
Chain of Ownership
When one object exists as an instance field inside another, you should release the "owned" (or "child") object when the "owner" (or "parent") object goes out of existence. This is done in the dealloc method of the parent object:
- (void) dealloc {
[child release]; // child was declared as an instance variable
[super dealloc];
}
Lifetime of the Program
When an object is intended to exist for the lifetime of the program, it usually isn't necessary to call release at all, unless some kind of resource cleanup needs to occur. You can put this in applicationWillTerminate:, which you can look up in Apple's documentation.
(You should probably avoid having such objects, but that is a discussion for another question.)
You have to think in terms of ownership. When you take ownership of an object by calling alloc, new or retain, you're also responsible for releasing it, either by calling autorelease when you return an owned object to the caller, or by calling release.
A general rule is:
Local variable: release it within the same method. When you want to return it to the caller, use autorelease
Class member: release it in the dealloc method