On 9 Aug 2008, at 17:39:20 -0600, Jonathan deWerd <[EMAIL PROTECTED]>wrote:


On Aug 9, 2008, at 4:48 PM, Cate Tony wrote:

This code is leaking:

- (void)saveItemExtensions:(id)sender
{
   NSMutableString* itemExtensionsFilePath = [NSMutableString
stringWithString:@"~/Library/Preferences/MyApp/extensions.abc"];
   NSDictionary* extensions = [NSDictionary dictionaryWithDictionary:
[itemDataSource itemExtensions]];

   [itemExtensionsFilePath setString:[itemExtensionsFilePath
stringByExpandingTildeInPath]];
   [[NSArchiver archivedDataWithRootObject:extensions]
itemExtensionsFilePath atomically: YES];
}

- (void)encodeWithCoder:(NSCoder *)coder
{
   [coder encodeObject: string1];
   [coder encodeObject: string2];
   [coder encodeObject: string3];
   [coder encodeObject: string4];
   [coder encodeObject: string5];
   [coder encodeObject: string6];
   [coder encodeObject: string7];
   [coder encodeObject: string8];
}
According to MallocDebug, the leak is in the encodeWithCoder method.

Things I've tried:

1 [coder encodeObject: [string1 autorelease]];  Which, of course,
caused a crash
2 [coder encodeObject: [string1 copy]];  no diff
3 [coder encodeObject: [[string1 copy] autorelease]];  no diff
4 Drinking beer...

Can anyone explain this to me?

Tony


What is coder? It could be that coder is deferring the encoding
(saving copies of string*), and then the coder itself is never getting
released. Have Instruments make sure that the coder is actually going
away after the encoding is done. Also check to make sure the encoded
data is going away when it is no longer needed. Sometimes the leak
isn't exactly where MallocDebug says the object was allocated: -retain
increments the retain count as surly as -alloc. What do the individual
string* histories look like in Instruments? Speaking of Instruments,
could you give us (not the mailing list, I don't think it allows
attachments, but you could email me separately) an instruments session
to look at? It would greatly facilitate proceedings :)

Also, why are you using non-keyed encoding? -encodeObject:forKey: is
the preferred way of doing things nowadays...

Well, it may be the preferred way, but it is not always the best, nor even the correct way.

Two reasons:
1. the resulting files are much bigger
2. NSKeyedArchiver can only store certain strings (tested in 10.4.11), which makes it absolutely unusable for the storage of strings if the possible values are not known in advance.


Kind regards,

Gerriet.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to