1

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.

Community
  • 1
  • 1
amir
  • 13
  • 2

1 Answers1

3

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
  • 48,104
  • 19
  • 94
  • 118
  • 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 Jun 06 '10 at 21:07
  • Read http://developer.apple.com/mac/library/iPad/index.html#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html – kubi Jun 07 '10 at 00:28
  • Generally, unless you use 'alloc', 'copy' or 'new' to create an object, you don't own it. – kubi Jun 07 '10 at 00:30
  • ... 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 Jun 07 '10 at 00:32
  • **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 Jun 07 '10 at 07:33
  • 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 Jun 07 '10 at 08:41
  • @JeremyP, your second phrase is what I meant, leaning this stuff is certainly important. – kubi Jun 07 '10 at 14:16