I’ve got a question. Where is the reason for EXC_BAD_ACCESS in the following code ?
-(void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
if(metadata.isDirectory) {
db_Path = metadata.path;
int i = 0;
NSString *fileName = [[NSString alloc] init];
for(DBMetadata *file in metadata.contents) {
fileName = [NSString stringWithFormat:@"%@", file.filename];
[db_MetaFileNames addObject:file.filename];
i++;
}
[self createMetaListArray];
[fileName release];
}
}
-(void)createMetaListArray {
fileNamesAtDirectory = db_MetaFileNames;
for (int i=0; i < [fileNamesAtDirectory count]; i++) {
NSString *filePathWithName = db_directory;
[filePathWithName stringByAppendingFormat:
[fileNamesAtDirectory objectAtIndex:i]];
[filePathsAtDirectory addObject:filePathWithName];
[filePathWithName release];
}
}
Can Anyone here help me ?
Here:
The
NSStringon the first line gets overwritten with the new values on the third line. The original value leaks.This means that:
The release on the last line releases not the
fileNamethat you alloc/init above, but the assignment inside the loop. You don’t alloc/copy/retain that, so you’re not “in charge” of releasing it.You have a similar misunderstanding in the second function.
This does not amend
filePathWithName. It returns a new string.I suggest you read up on Cocoa’s memory management rules — you’re missing some fundamentals. Understanding those will make your life a lot easier.