Illep
Illep

Reputation: 16841

Beginner memory management

I need to know if the retaincount of objects should always be 0 if i want to have a good memory management in my code. I got the following code from a book. and there's a statement NSLog called after release = 2, So should i have to release it 2 more times so that the retaincount will be 0 ?

#import <Foundation/NSObject.h>
    #import <Foundation/NSAutoreleasePool.h>
   #import <Foundation/NSString.h>
 #import <Foundation/NSArray.h>
 #import <Foundation/NSValue.h>
 int main (int argc, char *argv[])
 {
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 NSNumber *myInt = [NSNumber numberWithInteger: 100];
 NSNumber *myInt2;
 NSMutableArray *myArr = [NSMutableArray array];
 NSLog (@”myInt retain count = %lx”,
 (unsigned long) [myInt retainCount]);
 [myArr addObject: myInt];
 NSLog (@”after adding to array = %lx”,
 (unsigned long) [myInt retainCount]);
 myInt2 = myInt;
 NSLog (@”after asssignment to myInt2 = %lx”,
 (unsigned long) [myInt retainCount]);
 [myInt retain];
 NSLog (@”myInt after retain = %lx”,
 (unsigned long) [myInt retainCount]);


 NSLog (@”myInt2 after retain = %lx”,
 (unsigned long) [myInt2 retainCount]);
 [myInt release];
 NSLog (@”after release = %lx”,
 (unsigned long) [myInt retainCount]);
 [myArr removeObjectAtIndex: 0];
 NSLog (@”after removal from array = %lx”,
 (unsigned long) [myInt retainCount]);
 [pool drain];
 return 0;
 }

Program Output

myInt retain count = 1
after adding to array = 2
after asssignment to myInt2 = 2
myInt after retain = 3
myInt2 after retain = 3
after release = 2
after removal from array = 1

UPDATE

The following code was taken from the Apples memory management document. They have retained a NSNumber object and its never been released, is this OK ?

- (void)setCount:(NSNumber *)newCount {
[newCount retain];
[_count release];
// Make the new assignment.
_count = newCount;
}

Upvotes: 0

Views: 118

Answers (2)

NJones
NJones

Reputation: 27147

This is not to take away from existing answers, but to address your edit (Which, by the way, should REALLY be a separate question.):

They have retained a NSNumber object and its never been released, is this OK ?

- (void)setCount:(NSNumber *)newCount {
[newCount retain];
[_count release];
// Make the new assignment.
_count = newCount;
}

Well it wouldn't be, if that was what they did. You are confusing pointers with objects. Do you see the (NSNumber *) just before newCount. That '*' means that newCount is a pointer to an NSNumber object. Allow me to translate.

[newCount retain];

Retain the object referred to by the pointer 'newCount'.

[_count release];

Send a release message to the object currently referred to by the '_count' pointer.

_count = newCount; 

Make the pointer '_count' refer to the object that is referred to by the pointer 'newCount'

Think of it like this, if you have a car you likely just call it myCar. When you say myCar you mean your car, the car you own. Let's say you buy a new car for a while you will call it newCar so that you can tell the difference. So you take ownership of your new car [newCar retain], and for a brief moment you own two cars. Now you sell your old car, which you still call myCar, [myCar release](very sad). But now when you say myCar you mean your newCar myCar = newCar. You no longer own your old car. And now your newCar is just myCar and you own, retain, that one. When you die, dealloc, your car will be sold,[myCar release].

Upvotes: 1

Jesse Rusak
Jesse Rusak

Reputation: 57168

You shouldn't be worried about the retain count of your objects, especially because the the NSArray or other objects might retain and then only later release things you pass to them. I'd strongly suggest focusing instead on Objective-C and Cocoa's memory management rules, which ensure that things are cleaned up when needed.

Upvotes: 5

Related Questions