Re: Problem using filewrappers under snow leopard
Re: Problem using filewrappers under snow leopard
- Subject: Re: Problem using filewrappers under snow leopard
- From: Graham Cox <email@hidden>
- Date: Thu, 8 Oct 2009 11:56:19 +1100
On 08/10/2009, at 3:33 AM, Eagle Offshore wrote:
Oops, I misspoke. I am doing it "right" AFAICS. Here's the whole
method
- (NSFileWrapper *)fileWrapperOfType:(NSString *)aType error:
(NSError**)errPtr
{
int i, count = [_songs count];
NSMutableDictionary * fileWrappers = [NSMutableDictionary
dictionaryWithCapacity:count + 1];
[fileWrappers setObject:[[NSFileWrapper alloc]
initRegularFileWithContents:[self setData]] forKey:@"Sets.plist"];
leak
for(i = 0; i < count; ++i)
{
JBSong* song = [_songs objectAtIndex: i];
[fileWrappers setObject: [[NSFileWrapper alloc]
initRegularFileWithContents:[song asData]] forKey: [[song fileName]
stringByAppendingString: @".sng"]];
leak
[fileWrappers setObject: [[NSFileWrapper alloc]
initRegularFileWithContents:[song lyricData]] forKey: [[song
fileName] stringByAppendingString: @".rtf"]];
leak
}
[fileWrappers setObject: [[NSFileWrapper alloc]
initRegularFileWithContents:[NSKeyedArchiver
archivedDataWithRootObject: _midiController]] forKey: @"Midi.map"];
leak
return [[NSFileWrapper alloc]
initDirectoryWithFileWrappers:fileWrappers];
leak
}
And as I said, this worked in Tiger, and Leopard, and only now it
has a problem in Snow Leopard and the problem seems to be deep in
framework code. A console log message giving a clue to the cause of
the error would not be out of line, Apple.
The overall work this is doing looks fair, but it's leaking all over
the place.
Every time you do an alloc + init, the object you get is retained.
Whenever you do a -setObject:forKey:, it is retained, so you should
then do a -release to balance the original retain (the objects are
thus owned by the dictionary). The last line also leaks, because alloc
+ init... returns a retained object. Here you should do an autorelease
so you can balance the retain but allow the object to live for a while
longer so that the caller can use it.
If this is representative of the code in your app, you have big, big
problems. It could be that the bug you're experiencing is actually
elsewhere, due to non-existent memory management.
There's another problem too. You're using -stringByAppendingString: to
append the file extension. You should use -
stringByAppendingPathExtension: so that it takes care of any
subtleties with doing that. I also notice that you're setting a
filetype as a plist "Sets.plist" - are you sure that the data is
actually formatted as a plist?
My app also writes a file wrapper for its files which is really a
directory (package file). It works fine on 10.5 on 10.6, so I had a
look to see how mine differs from yours. I do this, pared down to its
essentials:
NSFileWrapper* fw = [[NSFileWrapper alloc]
initDirectoryWithFileWrappers:nil];
[fw addRegularFileWithContents:primaryContent
preferredFilename:@"file1.pri"];
[fw addRegularFileWithContents:secondaryContent
preferredFilename:@"file2.sec"];
[fw addRegularFileWithContents:tertiaryContent
preferredFilename:@"file3.ter"];
return [fw autorelease];
In other words I let -addRegularFileWithContents:preferredFilename: do
all the work rather than assembling it by hand. You could try revising
your code and see if that helps - you'll have to revise it
substantially anyway to fix all the leaks.
--Graham
_______________________________________________
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