tags:

views:

77

answers:

3

Possible Duplicate:
NSNumber retain count issue

Hello, I have the following code:

 NSNumber *number = [NSNumber numberWithInt:5];
 int i = [number retainCount];
 [number release];
 i = [number retainCount];
        [number release];
        i = [number retainCount];

the problem is that in line 2 , the value of parameter i is 2 and in line 4 the value is 1. then in line 6 the value is still 1.????????

first i dont understand why after init *number the retaincount is 2 and not 1?? second i dont understand why after release it 2 times retaincount is not 0? it doesnt matter how many times i release the object the retaincount stay 1.

+2  A: 

The problem is that you should never look at the 'retainCount' of an object unless you really know what you're doing, it'll only confuse you.

What's going on is that NSNumber is doing something behind the scenes. I'm not sure what, and I usually don't care. If I create an abject, I'm responsible for releasing it. As long as I fulfill my responsibilities, everything will work out as it should.

In your specific example, you're getting a reference to an 'NSNumber' and releasing it twice. Since you don't own that object, you should not be releasing it at all.


To clarify, the reason you shouldn't be looking at retain counts is that it frequently will mislead you. To quote @chuck from the link in the comments.

If you suspect you have a leak, you should check with the real debugging tools meant for that purpose, not by poking at retain counts. And for code you're writing, you should primarily be concerned with following the guidelines I linked above.

kubi
first thanks for the help.i didnt understand why i am not own that object (i create it!)does there are object that i create and didnt need to release? like object that dead in the end of function ?how can i know if object that i create was init with autorelease or not?thanks
amir
Read http://developer.apple.com/mac/library/iPad/index.html#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
kubi
Generally, unless you use 'alloc', 'copy' or 'new' to create an object, you don't own it.
kubi
... And don't worry about not getting this, it mat be the most difficult part of obj-c for folks new to the language.
kubi
**Do** worry about not getting it. Until you understand how memory management works, your apps will either leak like a sieve or crash due to over releasing. **Don't** worry about the fact that you find it confusing, everybody does at first.
JeremyP
agree with JeremyP, it can be tricky to learn, but once u do, it makes sense. numberWithInt is a class method of NSNumber, and will return an autoreleased object. So your first line would be the equivalent of doing [[[NSNumber alloc] initWithNumber:5] autorelease]
pxl
@JeremyP, your second phrase is what I meant, leaning this stuff is certainly important.
kubi
A: 

the same in this code

look at object range first i alloc the object (i believe the ref count is 1) then in the end i release it (i think the ref count need to be zero) so how the line : int i = [range intValue]; , is working?? by this time the range object need to be deallocated because previous line was released the object . i assumed that line int i = [range intValue]; was need to fail.

what i am missing?

-(NSString*)ParseLocationToString { NSNumber *range; NSString *locationAsString; locationAsString = [[NSString alloc] init];

locationAsString = m_Latitude;
locationAsString = [locationAsString stringByAppendingString:@","];
locationAsString = [locationAsString stringByAppendingString:m_Longtitude];
locationAsString = [locationAsString stringByAppendingString:@","];
range = [[NSNumber alloc] initWithInteger:m_RangeForResult];
locationAsString = [locationAsString stringByAppendingString:[range stringValue]];

[range release];
int i = [range intValue];

return locationAsString;

}

amir
Don't do it; just because it works for you right now, it doesn't mean that it will *always* work. It could be that `NSNumber` has a cache of frequently used numbers, and it gives you one of these instances instead of an actual new one, but don't care about it; just follow the memory management patterns.
dreamlax
A: 

so generaly the steps that increment the retain counter are:

  1. i used alloc.

  2. i used property that have the retain attribute.

  3. every initWith*** function ?????(i am not sure)

how do i know if frame work built in function that returns objects (for example stringByAppndString , numberWithDoble , etc... ) were return new object with retainCount = 1 , so i need to remember to release ?.

I am looking for few basic guidance to when i need to use release.

thanks

Amir
So how do i know when object that i create is autorelease??
Amir
Lets take for example this line:NSNumber *number = [NSNumber numberWithInt:5];how do i know that *number is now a new object with retaincount of 1? , there is no Create,New,Alloc in the name of the function.
Amir
Amir, at this point you should not be worrying about the retain counter at all. If you create an object with alloc, copy or new, you own the object and you must release it. If you call retain on an object, you own it and must release it. Otherwise, that object is not your responsibility and you don't need to worry about releasing it. Read the link I posted earlier, it will help clear a lot of this stuff up.
kubi