-1

Possible Duplicate:
Reference count is still 1 after [obj release], when it should be deallocated

1.When i write this code.

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
[self.view addSubview:label];
label.text =@"label Text";

 [label release];
 [label release];

 NSLog(@"LableRetainCount = %i \n",lable.retainCount);

Output: LableRetainCount 1.Lable retain count not decrease from 1 why?

2.When i write this code .

   UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
   [self.view addSubview:lable];
   label.text =@"lable Text";

  label = nil;
  NSLog(@"LabelRetainCount = %i \n",label.retainCount);

Output : LabelRetainCount = 0 When i set "label = nil" it's retain count become 0 why? it is meanes memory deallocated of this object?

  1. Object released or not?
  2. Now we are not need release it?
  3. The retainCount method can never return 0.It is means object alive after release?
Community
  • 1
  • 1
Hardeep Singh
  • 942
  • 8
  • 15

1 Answers1

1

in #2 it returns 0 because you set your label pointer to nil, which is 0, and when you call a function on nil it returns nil, ie 0. So its not really returning a retainCount its just returning nil. You have to understand after you set label to nil that it is no longer pointing at your UILabel...

On #1 if you wanted to properly release it, like all the way to 0, first you would do this after your .text line:

[label removeFromSuperview]; // remove the view that you added, view will release it
[label release];

Then your retain count should be 0 and the object will be released.

Codezy
  • 5,540
  • 7
  • 39
  • 48
  • 4
    `retainCount` will never return 0. – jscs Oct 09 '12 at 17:37
  • But retainCount never become 0 why? when you release it.but when you dealloc it, then it's retain count become zero.Release decrease the retain count but never set zero why? – Hardeep Singh Oct 09 '12 at 17:39
  • Object Retain Count 1 it is means object alive. – Hardeep Singh Oct 09 '12 at 17:44
  • @Hardeep, please look at the duplicate question I've linked. – jscs Oct 09 '12 at 17:46
  • When the retain count transitions to zero, the object is deallocated. Messaging a deallocated object yields undefined behavior. Therefore, the retain count can never be zero. – bbum Oct 09 '12 at 20:04
  • What you want to know in the end is that the object is dealloc'd - you can put a message in the dealloc noting that its gone out to ensure you have it right. – Codezy Oct 09 '12 at 20:23