开发者

Objects deallocation question

I have a code:

@interface PhotoListViewController : UIViewController 
{
    IBOutlet UIImageView* firstPicContainer;
    IBOutlet UIImageView* secondPicContainer;
    UIImage* pic_one;
    UIImage* pic_two;
    IBOutlet UILabel* firstPicLabel;
    IBOutlet UILabel* secondPicLabel;
    NSString* firstPicName;
    NSString* secondPicName;
    IBOutlet UILabel* authorLabelA;
    IBOutlet UILabel* authorLabelB;
    NSString* authorName;

}
@property (retain) UIImage* pic_one;
@property (retain) UIImage* pic_two;
@property (copy) NSString* firstPicName;
@property (copy) NSString* secondPicName;
@property (copy) NSString* authorName;


@end

The question is: should I release the whole pack of objects or just those, whose properties I've declared? For example: firstPicName is NSString and is retained, when is set, its container is UILabel* firstPicLabel, which is ass开发者_JS百科igned like this:

firstPicLabel.text = firstPicName;

in -viewDidLoad. So label as an object is not retained by itself, its property does. The same thing is with other objects, properties are retained, others just a container. The owner of these containers is theirs view, which should be responsible for their release, but I'm slightly confused here, so I want to clarify that point. Thanks.


Short answer: For any property that you declare as retain (or copy), you should set its value to nil in your dealloc method. Properties that are declared on other objects and fields which you do not retain yourself you do not need to worry about.


Your interface will never provide enough information about what you should or shouldn't release (but it will provide clues).

You could have all the ivars and properties you declared above, but if you haven't initialised them and/or accessed their properties, there is never going to be an object to release in the first place.

Rule of thumb is:

1) If you create, copy or retain an object, you are responsible for releasing it.

2) See rule #1 above.

In practice, here are some examples:

@implementation...
...
pic_one = [UIImage imageNamed:@"test.png"];
...
@end

pic_one is declared as an ivar but is assigned to an autoreleased object. You don't have to release it.

Another example:

@implementation....
...
self.pic_one = [UIImage imageNamed:@"test.png"];
...
@end

You assigned this UIImage object via its assessor/property which is declared as retain, so you are responsible for releasing it. "Behind the scenes", here's what happens when you assign the object via its property (example is for properties declared as retain, copy will be slightly different but same principle).

- (void)setPic_One:(UIImage*)newImage
{
   if (pic_One != newImage)
   {
        [newImage retain];
        [pic_One release];
        pic_One = newImage;
   }
}

And a last one:

UIImage *testImage = [[UIImage alloc] initWithImage:@"test.png"];
self.pic_one = testImage;
[testImage release];

In this case you are alloc'ing/init'ing (creating) an UIImage object, so you are responsible for releasing it. At the same time you are also assigning it to a property declared as retain so it is your responsibility to release it too when you are done with it (usually in your dealloc method).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜