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)];
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
.
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.
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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With