• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: When does an object own its instance variable?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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:37:21 +0100

Sorry should of mentioned, to understand memory allocation better, I would recommend reading the Objective C guide on apple's website. It explains the conventions used when getting objects, to sum it up

If you create an object via alloc, copy, copyWithZone, then you are responsible for releasing the object. If you get the object via other means, then the object which gave you the object is responsible for deleting it (In most cases it is autoreleased)

In this case I own the object

NSString *string = [[NSString alloc] init];  // Has a retain count of 1
[string release];						// Is deallocated

In this case I don't own the string

NSString *string = [NSString stringWithString:@"hello"]; // Is an autoreleased string
[string release]; // Oops, I am releasing it, and I don't own it, this will probably crash


The correct way would be
NSString *string = [[NSString stringWithString:@"hello] retain]; // Probably has a retain count of 2
[string release]; // Now has a retain count of 1
// Eventually it will be autoreleased by the object that created it, and the retain count will be 0 (Correct)


Tony



On 17 Aug 2007, at 11:26, Tony Wroblewski wrote:

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

_______________________________________________

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


  • Follow-Ups:
    • Re: When does an object own its instance variable?
      • From: mmalc crawford <email@hidden>
References: 
 >When does an object own its instance variable? (From: Bob Ueland <email@hidden>)
 >Re: When does an object own its instance variable? (From: Tony Wroblewski <email@hidden>)

  • Prev by Date: Re: How to enable menu items for the submenu of NSPopupButton?
  • Next by Date: Re: Inheritance question
  • Previous by thread: Re: When does an object own its instance variable?
  • Next by thread: Re: When does an object own its instance variable?
  • Index(es):
    • Date
    • Thread