Reputation: 2626
Is there a memory leaks when I set an attribut in this way :
titleView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 0, 300, 5)];
And is there a difference with
UIWebView *newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 0, 300, 5)];
[self setTitleView:newWebView];
[newWebView release];
Thanks,
EDIT : I'm releasing the titleView in the dalloc function
Upvotes: 0
Views: 89
Reputation: 69499
Assuming that you have declared the proerty correctly:
@property (nonatomic, retain) UIWebView *titleView;
if do:
self.titleView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 0, 300, 5)];
then you leak but when you do it direct in the ivar like:
titleView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 0, 300, 5)];
The you are ok.
But you need to release it in the dealloc:
- (void)dealloc {
[titleView release], titleView = nil;
[super dealloc];
}
Upvotes: 0
Reputation: 49354
Do not confuse attribute with instance and/or local variable. An attribute (or property) is accessed via self
, while instance variable is accessed directly by it's name.
In first example there's a leak since I can't see the release message sent to titleView
. If titleView
is instance variable you'd release it in viewDidUnload
method. If it's local - you should release it when adding to some view (like in second example).
In the second example there is no memory leak.
Upvotes: 0
Reputation: 4363
Assuming you have a property called titleView.
@property (retain) titleView
First one leaks, unless you release it on dealloc (but beware if you are assigning it more than once)
correct one should be:
self.titleView = [[[UIWebView alloc] initWithFrame:CGRectMake(10, 0, 300, 5)] autorelease];
it is always good practice to use self.propertyName as it also releases the old value.
Upvotes: 1
Reputation: 17877
If titleView
is declined without retain attribute then there is difference.
In first case there will be all ok. In second one - you can't use titleView
after [newWebView release]
.
Upvotes: 0