Re: NSUserDefaults won't do as it's told
Re: NSUserDefaults won't do as it's told
- Subject: Re: NSUserDefaults won't do as it's told
- From: email@hidden
- Date: Sun, 4 Mar 2007 08:51:04 +0100 (CET)
- Importance: Normal
>Also, you leak a lot in this code.
Yes. Since this must be the 1001th time I get it wrong on memory
management, and it would not be right for me to ask you to do
my homework, could you tell me if the following analysis about
the code in this "initialize" function is correct :
" The NSUserDefaults instance is the object that stores the
various data (yellowData, greenData, headerData, etc) created here, and is
therefore responsible for owning them, retaining them and
releasing them in due time. In pseudo-code the basic pattern should be :
pieceOfData=[(...) alloc ]initWithSomething:(...) ];
[[NSUserDefaults standard] register pieceOfData]; // NSUserDefaults assumes
//ownership of pieceOfData
[pieceOfData release]; // to balance the first line
"
Here the situation is more complicated because there is
an intermerdiary object "defaultValues". By looking at the retainCounts in
the debugger I saw that the line
[[NSUserDefaults standardUserDefaults] registerDefaults: defaultValues];
actually increments the retainCounts of the pieces of data, i.e. it stores
and retains those pieces directly instead of storing the defaultValues
object. This is
not obvious at all from the documentation of the registerDefaults: method.
This is a frequent problem I have with memory management : whenever
I use a new class or method I'm not familiar with, I ask myself: what does
it do to the retain counts of the objects it works on ? As in this
registerDefaults:
example, the documentation of the method is sometimes mute on the issue,
and all you
can do is make uncertain guesswork by looking at the retain counts in the
debugger.
Ewan
My corrected version of the code :
+(void) initialize
{
NSMutableDictionary* defaultValues=[NSMutableDictionary dictionary];
NSAttributedString* headerString=[[NSAttributedString alloc]
initWithString:@""];
NSAttributedString* footerString=[[NSAttributedString alloc]
initWithString:@"\n%"];
NSData* yellowData=[NSKeyedArchiver archivedDataWithRootObject:[NSColor
yellowColor]];
NSData* greenData=[NSKeyedArchiver archivedDataWithRootObject:[NSColor
greenColor]];
NSData* headerData=[NSKeyedArchiver
archivedDataWithRootObject:headerString];
[headerString release];
NSData* footerData=[NSKeyedArchiver
archivedDataWithRootObject:footerString];
[footerString release];
[defaultValues setObject:headerData forKey:GGPeggyHeaderContentKey];
[headerData release];
[defaultValues setObject:footerData forKey:GGPeggyFooterContentKey];
[footerData release];
[defaultValues setObject:yellowData forKey:GGPeggyHeaderColorKey];
[yellowData release];
[defaultValues setObject:greenData forKey:GGPeggyFooterColorKey];
[greenData release];
[defaultValues setObject:[NSNumber numberWithBool:YES]
forKey:GGMultiplePageViewBooleanKey];
[[NSUserDefaults standardUserDefaults] registerDefaults: defaultValues];
}
> Rosyna Keller
> Technical Support/Carbon troll/Always needs a hug
>
> Unsanity: Unsane Tools for Insanely Great People
>
> It's either this, or imagining Phil Schiller in a thong.
>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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