私はxcode/cocoa、さらにはobjective-cも初めてなので、私の質問はばかげているかもしれません。フォルダー内のファイルをハッシュするプログラムを作成しようとしています。周りを見回して、NSData オブジェクトを介してファイルをロードし、そのバイトを CC_SHA512 でハッシュする方法を見つけました。
さらにいくつかのファイルをハッシュしようとすると、メモリが不足していることに気付きました。Run -> Performance Tools を使用して、問題を特定できました。私が作成したすべての NSData オブジェクトは、まだメモリ内にあります。release / dealloc を使用して、autorelease と manualy release を試しました。何も機能していません。
私のコンパイラ設定は標準ですが、Objective-C Garbage Collection = required を選択する例外が 1 つあります。
誰かが私が間違っていることを教えてくれるかもしれません。
コードは次のとおりです。
-(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;
}
このすべては、によって開始されます
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];
}
多分誰かがアイデアを持っています。
事前にあなたに感謝します:) Nubus