views:

71

answers:

3

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];?

+2  A: 

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.

mmc
+1  A: 

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.)

Gregory Higley
Please be noted that `applicationWillTerminate:` is not called in iOS 4 most of the time.
ohho
Good call. It's not a method I typically need anyway.
Gregory Higley
+1  A: 

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

Philippe Leybaert