开发者

releasing object created in getter and setter

开发者 https://www.devze.com 2023-04-10 04:46 出处:网络
I have some doubt regarding retain in .h file. I know that if we alloc/copy/retain than we need to release it, but in following case

I have some doubt regarding retain in .h file. I know that if we alloc/copy/retain than we need to release it, but in following case

@propert开发者_运维问答y (nonatomic, retain) IBOutlet UITableView *myTable;

Do I need to release this table view object in my dealloc. I have created this tableview using xib.

Thanks.


So sayeth the docs:

Objects in the nib file are created with a retain count of 1 and then autoreleased. As it rebuilds the object hierarchy, however, UIKit reestablishes connections between the objects using the setValue:forKey: method, which uses the available setter method or retains the object by default if no setter method is available. If you define outlets for nib-file objects, you should always define a setter method (or declared property) for accessing that outlet. Setter methods for outlets should retain their values, and setter methods for outlets containing top-level objects must retain their values to prevent them from being deallocated.

And:

When a low-memory warning occurs, the UIViewController class purges its views if it knows it can reload or recreate them again later. If this happens, it also calls the viewDidUnload method to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded with the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and added to the view hierarchy. Typically, if your view controller contains outlets (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to relinquish ownership of those outlets or any other view-related data that you no longer need.

So basically, when being loaded from a NIB/XIB, the property is used. Meaning, if you specify retain properties on your IBOutlets (which you should), you need to release them. The preferred way to do this is in viewDidUnload, using the property.

@property (nonatomic, retain) IBOutlet UITableView *myTable;

...

- (void) viewDidUnload
{
    self.myTable = nil;
}


Yes you will have to as you would be creating an object in the .h file and allocating it memory.. The only thing you are doing in XIB is creating a link between the two (XIB just acts as an outlet for the inner tableview) , but if you posted a button using the xib and did not link it via the code then you don't have to release it...


First of all you are not retaining anything in .h file.

The purpose of @property declaration in the .h file (it can also be don in .m file) is to tell the compiler how to handle the getters and setters for this property when you use (dot syntax).

Example:

Declaring property in the following way:

@property (nonatomic, retain) IBOutlet UITableView *myTable;

Tells the compiler that when you create a UITableView in your .m file like so:

- (id)initWithTable:(UITableView *)table
{
    self = [super init];
    if (self) {

        self.myTable = table;
    }
    return self;
}

Compiler will automatically know to retain it, and so you would also need to release it.

But if you would declare your property in the following way:

@property (nonatomic, assign) IBOutlet UITableView *myTable;

and created the tableView as in the previous example

- (id)initWithTable:(UITableView *)table
{
    self = [super init];
    if (self) {

        self.myTable = table;
    }
    return self;
}

The compiler would only assing the value of myTable to point to table. You would not own it and should not release it.


No u dont need to u only need to release object which you have allocated. since the table view is allocate in the xib it's release should be none of your concern hope this helps


it should be release. if you want to see difference then run your application in instruments and check it.

0

精彩评论

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

关注公众号