user372705
user372705

Reputation: 81

Obj-C Memory Management Setter Method

I am new to objective-c and I've downloaded the code from here.
Ran 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?

Upvotes: 2

Views: 254

Answers (5)

Kristopher Johnson
Kristopher Johnson

Reputation: 82535

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.

Upvotes: 3

user373455
user373455

Reputation: 13271

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.

Upvotes: 0

yehnan
yehnan

Reputation: 5452

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.

Upvotes: 0

Chuck
Chuck

Reputation: 237010

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.

Upvotes: 4

drawnonward
drawnonward

Reputation: 53659

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

Upvotes: 1

Related Questions