Re: When does an object own its instance variable?
Re: When does an object own its instance variable?
- Subject: Re: When does an object own its instance variable?
- From: Tony Wroblewski <email@hidden>
- Date: Fri, 17 Aug 2007 11:26:31 +0100
It looks flawed to me,
The "name = [NSString stringWithString: theName];" statement would
return you an autoreleased string, and if you don't retain it, it will
get released. As far as I know, if you get a string without calling
alloc or copy, then you don't own the object unless you retain it.
The other method "name = [[NSString alloc] initWithString: theName];"
is also incorrect, because he doesn't release the old name string
first, which would cause a memory leak.
I would do something like the following:
-(void) setName:(NSString *)theName
{
if (name)
[name release];
name = [theName copy];
}
Tony
On 17 Aug 2007, at 11:14, Bob Ueland wrote:
I'm reading Kochan's book "Programming in Objective-C". In chapter
15 he has the following piece of code:
interface AddressCard: NSObject
{
NSString *name;
...
}
-(void) setName: (NSString *) theName;
...
@end
@implementation AddressCard;
-(void) setName: (NSString *) theName
{
name = [[NSString alloc] initWithString: theName];
}
...
@end
Explaining the code he says; Defining the method this way:
-(void) setName: (NSString *) theName
{
name = [NSString stringWithString: theName];
}
would be the incorrect approach because the AddressCard method would
not own its name object, NSString would own it. I do not understand
it. Could somebody explain what he means?
Thanks Bob
____________________________________________________________________________________
Park yourself in front of a world of choices in alternative
vehicles. Visit the Yahoo! Auto Green Center.
http://autos.yahoo.com/green_center/
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden