问题
I have the following in the header:
@property (nonatomic, retain) UIView *overlay;
And in the implementation:
@synthesize overlay;
Then:
UIView *tempOverlay = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 70.0f, 150.0f, 310.0f)];
self.overlay = tempOverlay;
[tempOverlay release];
Isn't the tempOverlay
variable above unnecessary? Can't I just do:
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 70.0f, 150.0f, 310.0f)];
回答1:
A synthesized retained setter looks like :
- (void)setValue: (id)newValue
{
if (value != newValue)
{
[value release];
value = newValue;
[value retain];
}
}
In your case, you have two valid methods :
1) Create a temp var, alloc/init (= retained), set to property, release.
IView *tempOverlay = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 70.0f, 150.0f, 310.0f)];
self.overlay = tempOverlay;
[tempOverlay release];
2) No temp var, set directly to ivar.
overlay = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 70.0f, 150.0f, 310.0f)];
UPDATE:
If you use method 2), you have to explicitly handle the rest of memory management (not only retaining), by releasing any previous value it might have before if needed. If done only once in init
(for instance), you can just put a [overlay release];
in dealloc
.
回答2:
Using the retain
attribute specifies that retain
should be invoked on the new object, and the previous value is sent a release
.
So in your second code block, the retain count of the object would become 2, since you no longer release it, and the setter is retaining it. This is unlikely to be what you want.
回答3:
If you assign the object directly to the property, you still must release it:
self.overlay = [[[UIView alloc] initWithFrame:CGRectMake(160.0f, 70.0f, 150.0f, 310.0f)] autorelease];
回答4:
As your property is defined with (retain) any instance you set using the synthesized setter (via the self.overlay syntax) will automatically be sent a retain message:
// You're alloc'ing and init'ing an object instance, which returns an
// instance with a retainCount of 1.
UIView *tempOverlay = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 70.0f, 150.0f, 310.0f)];
// The overlay property is defined with (retain), so when you assign the new
// instance to this property, it'll automatically invoke the synthesized setter,
// which will send it a retain message. Your instance now has a retain count of 2.
self.overlay = tempOverlay;
// Send a release message, dropping the retain count to 1.
[tempOverlay release];
If you were to do:
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 70.0f, 150.0f, 310.0f)];
Your overlay would have a retain count of two, which will likely lead to a leak at some point in your application.
回答5:
Thank you for all the answers. I got some conflicting claims so I tested the following in a UITableViewController
:
- (id)initWithStyle:(UITableViewStyle)style {
if ((self = [super initWithStyle:style])) {
NSLog(@"count: %d", [overlay retainCount]);
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 70.0f, 150.0f, 310.0f)];
NSLog(@"count: %d", [overlay retainCount]);
[overlay release]; NSLog(@"released once");
NSLog(@"count: %d", [overlay retainCount]);
[overlay release]; NSLog(@"released twice");
NSLog(@"count: %d", [overlay retainCount]);
}
return self;
}
I got the following console output:
Sometimes it ran fine:
count: 0 count: 2 released once count: 1 released twice count: 1
Other times it crashed:
count: 0 count: 2 released once count: 1 released twice Program received signal: “EXC_BAD_ACCESS”.
I know the method using tempOverlay
is correct. It just seems so cumbersome, but I prefer it to autorelease
because I don't understand how autorelease
works or when it's called. One thing is for sure. The code above is wrong because I don't want overlay
to have a retain count of 2.
The weird thing is that I can't release it twice. Even when it doesn't crash, the retain count is not decremented.
Anyway, guess I'll stick with using the tempOverlay
for now.
回答6:
Yes, you can directly assign the newly created object to overlay
object. If you wish, you can prove it to yourself by printing out the retain count of the object
NSLog(@"count: %d", [overlay retainCount]);
来源:https://stackoverflow.com/questions/3924463/how-is-retain-setter-implemented-with-synthesize