Re: newbie memory question
Re: newbie memory question
- Subject: Re: newbie memory question
- From: pmcurry <email@hidden>
- Date: Sun, 30 Nov 2003 19:23:23 -0800
So it's alloc and copy that determine reesponsibility.
Thanks.
-Phil
On Nov 30, 2003, at 4:25 PM, Chris Hanson wrote:
On Nov 30, 2003, at 2:57 PM, pmcurry wrote:
NSData* data = [[notice userInfo] objectForKey:
NSFileHandleNotificationDataItem];
NSString* string = [[NSString alloc] initWithData: data encoding:
[NSString defaultCStringEncoding]];
NSArray* components = [string componentsSeparatedByString:@"\n"];
Can I assume these three lines are using 'convenience type' calls and
I'm not responsible for releasing any of the objects created in the
process? I'm still memory gun-shy.
No, you can't assume that. The memory management rules are pretty
clear on what all o the above is.
NSData *data = [[notice userInfo] objectForKey:
NSFileHandleNotificationDataItem];
This gets a value from the dictionary. There's no use of +alloc or
-copy so you have to assume you don't own the object and it's
autoreleased. If you want to own it you have to retain or copy it.
NSString* string = [[NSString alloc] initWithData:data
encoding:
[NSString defaultCStringEncoding]];
This instantiates a new string. Since there *is* a use of +alloc, you
own the string and are responsible for releasing it using either
-release or -autorelease. The string will be self-contained, with its
own copy of the data.
(Incidentally, using defaultCStringEncoding is generally a bad idea
because it can be different for different systems or even users on the
same system. Better to use a known encoding, that way you get
consistent results.)
NSArray* components = [string componentsSeparatedByString:@"\n"];
This gets an array of lines in the string you just instantiated.
Since there's no use of +alloc or -copy, you have to assume you don't
own the array (or its contents) and that it's autoreleased. If you
want to own it you have to retain or copy it.
-- Chris
--
Chris Hanson <email@hidden>
bDistributed.com, Inc.
Outsourcing Vendor Evaluation
Custom Mac OS X Development
Cocoa Developer Training
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.