저는 xcode/cocoa와 심지어 objective-c에 익숙하기 때문에 제 질문은 어리 석을 수 있습니다. 임 폴더에 파일을 해시하는 프로그램을 작성하려고합니다. 주위를 둘러 보았고 NSData 객체를 통해 파일을로드하는 방법을 발견했으며 해시보다 CC_SHA512를 사용하여 바이트를 가져 왔습니다.NSData 객체의 메모리를 비울 수 없습니다.
내가 더 많은 파일을 해시하려고하면 메모리가 부족하다는 것을 알았습니다. 실행 -> 성능 도구를 사용하여 내 문제를 정확하게 지적 할 수 있습니다. 내가 만든 모든 NSData 객체는 여전히 메모리에 있습니다. release/dealloc을 사용하여 autorelease 및 manualy 릴리스를 시도했습니다. 아무것도 작동하지 않습니다.
내 컴파일러 설정은 Objective-C 가비지 수집 = 필수를 선택한 한 가지 예외를 제외하고는 표준입니다.
누군가 내가 잘못하고있는 것을 나에게 보여줄 수 있습니다. 이 모든
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];
}
어쩌면 누군가가 아이디어를 가지고 시작한다
-(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;
}
: 여기
는 코드입니다. 사전에보다: 은 Nubus
안녕하세요, 링크를 이용해 주셔서 감사합니다. 설명서에는 편의 방법을 사용하는 경우 사용자가 말한 것처럼 릴리스를 사용할 필요가 없다고 명시되어 있습니다. NSData 객체가 hashFileByName 함수 외부에서 사용되지 않습니다. NSData 객체를 제거하는 방법을 알지 못합니다. (나는 문서 제트기를 사용하지 않습니다.) 그런 물체를 강제로 제거 할 수있는 방법이 있습니까? 감사합니다 – TheNubus
편리한 방법을 사용하면 자동 렌더링 된 객체를 다시 사용할 수 있습니다. 즉, 보유 개수가 0이라고 가정하면 다음 자동 회수 수집 시점에 메모리에 회수됩니다. 좀 더 명확히하고 싶다면/init/release를 할당해야합니다. – nall