Hi,
i'm new to xcode / cocoa and even objective-c thus my question might be stupid. Im trying to write a program that will hash files in a folder. I looked around and found a way to load a file via a NSData object and than hash it's bytes with CC_SHA512.
If i try to hash a few more files i noticed my memory running out. Using the Run -> Performance Tools i could pinpoint my problem. All NSData Objects i created are still in memory. I tryed autorelease and manualy release with release / dealloc. Nothing is working.
My Compiler Settings are standard with one exception i choose Objective-C Garbage Collection = required.
Maybe someone can show me what i'm doing wrong.
Here is the code:
-(FileHash*) hashFileByName :(NSString*) filePath{
//NSData* inputData = [inputStr dataUsingEncoding:NSUTF8StringEncoding];
NSLog(filePath);
NSData* inputData = [[NSData dataWithContentsOfFile:filePath] autorelease];
unsigned char outputData[CC_SHA512_DIGEST_LENGTH];
CC_SHA512([inputData bytes], [inputData length], outputData);
NSMutableString* hashStr = [NSMutableString string];
int i = 0;
for (i = 0; i < CC_SHA512_DIGEST_LENGTH; ++i)
[hashStr appendFormat:@"%02x", outputData[i]];
//NSLog(@"%@ hash : %@",filePath,hashStr);
FileHash *hash = [[[FileHash alloc]init]autorelease];
[hash setFileHash:hashStr];
[hash setFilePath:filePath];
[inputdata release];
[inputdata dealloc];
return hash;
}
-(NSMutableArray*) hashFilesInDirectory:(NSString*) pathToDirectory:(Boolean) recursive : (IBOutlet id) Status : (Boolean*) BreakOperation{
NSGarbageCollector *collect = [NSGarbageCollector defaultCollector];
NSMutableArray *files;
files = [[self listFilesOnlyRecursive:pathToDirectory] autorelease];
NSMutableArray *hashes = [[[NSMutableArray alloc]init]autorelease];
for (NSString *file in files) {
[hashes addObject: [self hashFileByName:file]];
[collect collectExhaustively];
}
return hashes;
}
-(NSMutableArray*) listFilesOnlyRecursive : (NSString*) startDir {
NSMutableArray *filelist = [[[NSMutableArray alloc] init]autorelease];
//Inhalt eines Verzeichnisses auflisten (unterverzeichnisse werden ignoriert
NSFileManager *manager = [[NSFileManager defaultManager]autorelease];
NSDirectoryEnumerator *enumerator = [manager enumeratorAtPath:startDir];
int count = 0;
id file;
while (file = [enumerator nextObject])
{
// file = [[[[startDir stringByAppendingString:@"/"]autorelease] stringByAppendingString:file] autorelease
// ];
file = [NSString stringWithFormat:@"%@/%@",startDir,file];
BOOL isDirectory=NO;
[[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDirectory];
if (!isDirectory){
[filelist addObject:file];
//printf("\n:%s:\n",[file UTF8String]);
count++;
}
}
NSLog(@"Es waren %i files",count);
return filelist;
}
All of this is started by
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//return NSApplicationMain(argc, (const char **) argv);
MemoryLeakTest *test = [[[MemoryLeakTest alloc]init]autorelease];
[test hashFilesInDirectory:@"/huge directory/" :YES :nil :nil];
[pool drain];
[pool release];
[pool dealloc];
}
Maybe someone has an idea.
Than You in advance :) Nubus