views:

88

answers:

5

I am new to objective-c and ive downloaded the code from here http://apress.com/resource/bookfile/4175 and ran the Chapter 10, 10.01 CarPartsInit xcode project file.

One thing i am not clear about is that, does the memory management of a setter method

- (void) setEngine: (Engine *) newEngine
{
    [newEngine retain] 
    [engine release];
    engine = newEngine;
}

supposed to result with the engine retainCount to be 0 at the end of the program.

I ran some NSLog and when the program ends... the retainCount for engine was at 1... Should it go to 0 and be freed? or is this a memory leak?

Thanks =)

+3  A: 

It is not necessary to release everything before the program exits. The operating system automatically reclaims all the memory the program used.

It is common to not worry about objects that exist for the duration of the program. You really only have a memory leak if you create something but then lose the reference before releasing it.

Note: You may run into idiots who claim that a program is somehow "wrong" if it doesn't clean up every single object before termination. You can ignore those people.

Kristopher Johnson
what if its on an iPhone, will the os take care of it? since it's crucial to avoid autorelease and also GC is not supported?Will it hog the application when these memories are not freed?
Does not matter. once a program is terminated, all resources related to the program are reclaimed.
bbum
@tmlee: The OS will reclaim your process's memory when it terminates. This is true on every modern computer platform I know of, including the iPhone.
Chuck
It is not really "crucial" to avoid autorelease. You may get better performance if you avoid it, but unless you are autoreleasing zillions of objects at a time, it's not really going to be a problem.
Kristopher Johnson
+1  A: 

Everything that an instance retains (or allocates) should be released in dealloc.

-(void) dealloc {
  [engine release]; // no need to set to nil in dealloc
  [super dealloc];
}
drawnonward
+4  A: 

You should not worry about retain counts. You shouldn't even look at them. They're just too confusing — they're really a private implementation detail. Case in point: No object's retain count ever goes to zero. Why? Because there's no point. An object with a retain count of 0 would be a deallocated object, and it's a bug to send messages to deallocated objects (it might report the old value of 1, or it might report something completely different, or it might just crash your program). As long as you're following the memory management guidelines and Instruments doesn't report any leaks, you should feel OK.

Chuck
A: 

Let's examine what setEngine should do.

  1. someone pass in a newEngine, and you wanna use it. So you need to "retain" it.
  2. you car maybe already have an engine. So you release it. (Note, send release message to null does nothing)
  3. then you assign passed-in argument "newEngine" to the car's property "engine". Done.

If you wanna see how does retain count increase and decrease, overwrite method "release" to print out something. But Remember to call super's release.

yehnan
A: 

While you have initialized your class, the engines memory count will increase with one (saying you do initialize it).
In your setter, the new value needs to be retained and the old value released (retain counter increases with new value, then counter decreases to get rid of the old value). The retain-counter is therefor still 1.

When closing the program, you need to also release the Engine in your dealloc-function, to be sure not get any memory-leaks.

A great way to watch for memory leaks is using the "Leaks"-tool in xcode:
run -> Start with Performance Tool -> Leaks

Other methods is to use the "Build and analyze" tool to find potential memory leaks:
Build > Build and Analyze

Hope I'm right with this, my objective-c is a bit rusty.

Fredrik_jakob