In the book I’m studying from for iPhone dev, they utilize IBOutlet instances using the Interface Builder. An example would be a UIButton. So they add a thing in the struct like this:
IBOutlet UIButton *whateverButton;
Then they add a @property for each of these in the .h, and a @synthesize in the .m.
Then they include a release in the dealloc of the .m. Two questions:
- Is the release necessary? Aren’t all properties already handled automatically?
- How can I check the ref count to see what’s happening, for debug purposes…?
If the property is retained, the release is necessary. When you declare a
@propertyand@synthesizeit, all you get is the accessors, there is no special automatic behaviour in dealloc.Also, there is nothing magical about IBOutlet – it’s just a marker for Interface Builder to see which properties you would like to appear in IB. It’s simply an empty macro, Cmd-click the IBOutlet keyword to see its definition:
Same thing goes for IBAction which expands to
void.When I need to debug memory management, I usually simply set up a breakpoint in the dealloc method or log a string there. It is also helpful to log the
retainCountof an object around the calls that might do something fishy with it.It might also help to see how the
@synthesizedirective creates the accessors. When you declare a retained@propertyand ask the compiler to@synthesizethem, you get something like this:This isn’t exactly the thing, but it’s close enough. Now it should be more clear why you should release in dealloc.