Need help tracking down memory issue
Need help tracking down memory issue
- Subject: Need help tracking down memory issue
- From: <email@hidden>
- Date: Thu, 10 Mar 2005 9:54:00 +0000
***WARNING-LONG POST***
First off, I sincerely apologize for the length of this post. Since I'm not sure what the culprit is, I'm copying as much relevant code as I can provide. But I've been looking at this code so long, I don't see any problems.
Okay, this has been plaguing me for quite some time, so I'm hoping someone can shed some light on this, tell me where I'm going wrong. Here's the info:
I have an NSArray ivar that contains NSStrings representing filenames. At first, the filenames are selected from an NSOpenPanel, and set as the ivar like so:
int result;
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:YES];
[openPanel setTitle:@"Choose files for processing"];
[openPanel setPrompt:@"Add files"];
result = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:nil];
if (result == NSOKButton)
{
NSArray* array = [[NSArray alloc] initWithArray:[openPanel filenames]];
[self setFilesToEncode:array];
/* ... */
[array release];
}
The setFilesToEncode looks like this:
- (void)setFilesToEncode:(NSArray*)newFilesToEncode
{
if (filesToEncode != newFilesToEncode)
{
[filesToEncode release];
[newFilesToEncode retain];
filesToEncode = newFilesToEncode;
}
}
I know this method is fine, I have other such methods for other arrays in my app and have no issue with them.
Okay, once that happens, the array is passed to a method like so:
[self handleFLACEncode:[self filesToEncode] withVerification:YES];
The withVerification is important because it calls another method that seems to be the one causing the problem. But I want to make you all see what's leading up to this point, in case it's the culprit.
In the above method, the array is iterated over. Each iteration creates an NSString like so:
int i;
NSMutableArray* flacConvertedFiles = [[NSMutableArray alloc] init];
for (i = 0; i < [files count]; i++)
{
// File to encode to flac, escape spaces if any
NSString* flacFileToConvert = [files objectAtIndex:i];
flacFileToConvert = [self escapeSpaces:flacFileToConvert]; // simply escapes any space characters
/* ... */
// outputPath is a previously set ivar from an NSSavePanel
NSString* newFileName = [[self outputPath] stringByAppendingString:[[[flacFileToConvert lastPathComponent] stringByDeletingPathExtension] stringByAppendingPathExtension:@"flac"]]; // long-winded way of changing the extension of the file
[flacConvertedFiles insertObject:newFileName atIndex:i];
/* a bunch of unrelated stuff happens, then we pass the flacConvertedFiles array to another method */
[self handleFLACFingerprint:flacConvertedFiles];
/* ... */
}
Now, when the above method runs and returns back to the enclosing method, flacConvertedFiles is released. Here is the entire handleFlacFingerprint method, as I don't know what's causing my app to crash. I will say that it does exactly what it intends to do, but sometime after this method executes, something happens and the app crashes.
- (void)handleFLACFingerprint:(NSArray*)convertedFiles
{
NSString* execPath = [[NSBundle mainBundle] pathForResource:@"metaflac" ofType:nil inDirectory:@"Binaries"];
int result;
NSSavePanel* savePanel = [NSSavePanel savePanel];
[savePanel setTitle:@"Choose file name"];
[savePanel setCanCreateDirectories:YES];
[savePanel setMessage:@"Please ensure the file is saved in the same directory as the .flac files\nyou are creating the fingerprint file for"];
result = [savePanel runModalForDirectory:[self outputPath] file:@"fingerprint.ffp.txt"];
NSString* fingerprintPath = [savePanel filename];
[[NSFileManager defaultManager] createFileAtPath:fingerprintPath contents:nil attributes:nil];
if (result == NSOKButton)
{
int i;
NSFileHandle* fileWriter = [NSFileHandle fileHandleForWritingAtPath:fingerprintPath];
[md5ProgressText setStringValue:@"Generating fingerprint file..."];
[NSApp beginSheet:md5ProgressSheet modalForWindow:mainWindow modalDelegate:self didEndSelector:NULL contextInfo:nil];
for (i = 0; i < [convertedFiles count]; i++)
{
NSString* fileToConvert = [convertedFiles objectAtIndex:i];
NSArray* arguments = [NSArray arrayWithObjects:@"--show-md5", fileToConvert, nil];
NSTask* task = [[NSTask alloc] init];
NSPipe* pipe = [[NSPipe alloc] init];
NSFileHandle* fileReader = [pipe fileHandleForReading];
[task setLaunchPath:execPath];
[task setArguments:arguments];
[task setStandardOutput:pipe];
[task launch];
[task waitUntilExit];
NSString* output = [[NSString alloc] initWithData:[fileReader readDataToEndOfFile] encoding:NSASCIIStringEncoding];
NSString* fingerprint = [[NSString alloc] initWithString:[fileToConvert lastPathComponent]];
fingerprint = [[fingerprint stringByAppendingString:@":"] stringByAppendingString:output];
[fileWriter seekToEndOfFile];
[fileWriter writeData:[fingerprint dataUsingEncoding:NSASCIIStringEncoding]];
[task release];
[pipe release];
[output release];
[fingerprint release];
// arguments = nil;
// [arguments release];
}
[fileWriter closeFile];
[md5ProgressSheet orderOut:nil];
[NSApp endSheet:md5ProgressSheet];
}
}
Now, I'm not sure if the passing of arrays all over is the issue. This also happens to be one of the only methods in my app that attaches a pipe to a task to handle output. All the other methods in my app use an NSTask to launch a UNIX tool to do the dirty work, and they all work fine, but none of them pipe the output for special handling, so I'm thinking that might have something to do with it as well.
Again, if you're still with me, I apologize for the length. This is my only resource to bounce these ideas/problems off of, so please bear with me. Thanks for any info you can give me.
James
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden