开发者

Memory Management of Interface Builder Outlets

开发者 https://www.devze.com 2022-12-12 18:35 出处:网络
I am using @property(nonatomic, retain) for my IBOutlets for an iPhone application. However, I\'m not sure how to make sure I\'m managing memory with them properly. The IBOutlets are all set up in Int

I am using @property(nonatomic, retain) for my IBOutlets for an iPhone application. However, I'm not sure how to make sure I'm managing memory with them properly. The IBOutlets are all set up in Interface Builder, so I'm never calling alloc manually. This means that I'm not sure when and if to deallocate them or when to set them to point to nil.

What are the best practices ensuring that no memory i开发者_如何学JAVAs leaked once the view unloads?


If you use @properties for yourIBOutlets and make the connections in IB then your controller is essentially retaining the IB objedcts with the property and is it therefore responsible for releasing them when it's done with them.

When are you done with them?

In every case you should be setting your properties self.propertyname = nil in your viewDidUnload method and again in dealloc of each viewController.

It's quite straight forward, IB manages everything else.


By default, the semantics of @property is retain, meaning that when the view controller loads the nib and connects IBOutlets, they get retaind by the @synthesized setter. Follow the standard rules of Cocoa memory managment: you must release these properties eventually or you will leak memory. dealloc is probably a good place to do this. On the iphone you can do this via self.outletProperty = nil. On OS X (when you're not using GC), the rules are the same, except you can use [self->outletProperty release] explicitly, bypassing the @synthesized setter.


You should do it like this....

[yourOutletVar release];
yourOutletVar = nil;

Please don't forget to set the IBOutlets to nil finally, because it is necessary to get rid of dangling pointers issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号