Re: [newbie] memory mgmt question
Re: [newbie] memory mgmt question
- Subject: Re: [newbie] memory mgmt question
- From: Fritz Anderson <email@hidden>
- Date: Sun, 21 Dec 2003 02:06:23 -0600
First, never send two init messages to an object. I don't know that
happens when you send an NSDictionary (an immutable object) an init (be
an empty dictionary) message, followed by an initWithContentsOfFile:
message, but I wouldn't be surprised if the dictionary remained empty.
(I'm assuming that "dict = initWithContentsOfFile:" is a typo
introduced while pasting "[dict initWithContentsOfFile:".)
Second, in your example the values in the dictionary are kept from
being released only by the dictionary itself. Releasing the dictionary
out from under a value, without retaining the value for yourself,
releases the value as well. The pointer "version" points to garbage
after [dict release].
Since your method isn't interested in preserving either the value or
the dictionary, do this:
NSString * retval = [[dict objectForKey: @"ProductVersion"]
substringToIndex: 4];
[dict release];
return retval;
-[NSString substringToIndex:] returns an autoreleased NSString, which
is what you intend to return.
Note, by the way, that SystemVersion.plist is an internal-use file, and
has only a sporting chance of being in future system builds. Gestalt is
the supported way to get this information.
-- F
On Dec 20, 2003, at 11:03 PM, jay wrote:
In the implementation I have a method like this:
- (NSString *)findOSVersion {
NSDictionary *dict = [[NSDictionary alloc] init];
// loads dict with
// /System/Library/CoreServices/SystemVersion.plist
dict = initWithContentsOfFile:[NSString
stringWithCString:SystemVersionPlist]];
NSString *version = [dict objectForKey:@"ProductVersion"];
[dict release]; // have tried it without the release
return [version substringToIndex:4]; // have tried with a
// retain
}
_______________________________________________
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.