0

Why IBOutlet retain count is 2 not just 1??

and what is the difference between

IBOutlet UILabel *fooLabel;

and

UILabel *fooLabel;


@property (nonatomic, retain) IBOutlet UILabel *fooLabel; 
JeremyP
  • 84,577
  • 15
  • 123
  • 161

4 Answers4

2

Why IBOutlet retain count is 2 not just 1?

You don't care. No, honestly, you don't. This is precisely why people will tell you not to ever worry about retain counts. You can never guarantee that it will be any particular number you expect. Retain counts are Cocoa internal implementation details. There's no reason why it shouldn't be 100 if the framework wants it to be, or even UINT_MAX.

and what is the difference between

IBOutlet UILabel *fooLabel;

and

UILabel *fooLabel;

@property (nonatomic, retain) IBOutlet UILabel *fooLabel;

The first declares an instance variable that can act as an outlet. The second declares a property that can act as an outlet. When the NIB is loaded, in the first case the pointer is assigned directly to the instance variable and in the second, the accessor is used to assign the instance variable.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • For the retain count, i have to release the outlets twice, the first on viewDiDUnload and then in dealloc, and i dont really understand why ??? – pointeur Mar 18 '11 at 11:11
  • @pointer: No you don't. Forget about retain counts. Your only concern is whether you own the object. Did you `alloc` it, `new` it, `retain` it or `copy` it? If the answer is no to all of those, you **must not** release it. – JeremyP Mar 18 '11 at 11:23
  • all i did is IBOutlet UIImageView *imageView; but i have to release it twice, i'm developing an iphone app and this is related to nib/xib files loading, something that i didnt' really understand – pointeur Mar 18 '11 at 11:28
  • @pointer: No, you don't have to release it at all. It's not your problem what the framework does. – JeremyP Mar 18 '11 at 11:30
2
  1. The absolute retain count value is irrelevant to your own memory management practices. Do not rely on it to diagnose memory management issues. You should check out Apple's documentation - link here

  2. As for your second question, here's a quick overview

IBOutlet UILabel *fooLabel; declares a fooLabel variable along with an outlet for your Interface Builder nib file.

UILabel *fooLabel; as above without the outlet for Interface Builder.

@property (nonatomic, retain) IBOutlet UILabel *fooLabel; declares a property fooLabel and an outlet for your nib file. If you synthesize this property with synthesize fooLabel, it will create a getter and setter methods for the property. The (retain) attribute tells the synthesized setter method to retain your new value before releasing the old one.

Rog
  • 18,602
  • 6
  • 76
  • 97
2

1) Do not use retainCount to reason about "retain state" of object - When to use -retainCount?

2) In both cases outlet object will be retained because of KVC (in the first case it's "magic"). That means that in both cases you have to release it when you're done with it (e.g. in dealloc).

3) Second snippet is guaranteed to work as intended, while behavior of the first one looks like implementation dependent to me (I can't find clear documentation on KVC for non-property ivars).

Community
  • 1
  • 1
hoha
  • 4,418
  • 17
  • 15
0

Check your code carefully whether you are explicitly retaining the label([fooLabel retain]). If not, then don't release it twice. Release it only in dealloc.

Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
  • When declaring IBOutlet UIImageView *imageView; i have to release it twice – pointeur Mar 18 '11 at 11:25
  • Did you checked your code with instruments? Got any leaks? If not don't worry. Just release it only in dealloc. If you release it twice you may get an EXC_BAD_ACCESS for double free the object, so be careful while releasing an object twice. – Adarsh V C Mar 18 '11 at 11:40