2011-08-21 4 views
0

빡빡한 루프에서 NSAutoreleasePool을 사용하고 있지만 아래의 메서드에서 다음 경고 행을 통해 메모리 경고가 발생하고 궁극적으로 내 앱이 충돌합니다. 가버 리다). 누구나 이것이 왜 그런지 생각할 수 있습니까?신비한 메모리 누수 - NSString 자동 해제 문제

filepath = [docpath stringByAppendingPathComponent:file]; 

-(void)fileCleanup 
{ 
    NSString *documentspath = [AppSession documentsDirectory]; 
    NSString *docpath = [documentspath stringByAppendingPathComponent:@"docs"]; 
    NSFileManager *filemanager = [NSFileManager defaultManager]; 
    NSArray *files = [filemanager subpathsOfDirectoryAtPath:docpath error:NULL]; 
    NSLog(@"fileCleanup"); 
    if(files != nil && [files count] > 0) 
    { 
     BOOL deletefile; 
     NSString *filepath; 
     NSAutoreleasePool *readPool; 
     NSString *testfile; 
     NSString *file; 

     for(file in files) 
     { 
      deletefile = YES; 

      for (testfile in allFiles) { 
       readPool = [[NSAutoreleasePool alloc] init]; 

       //line below is causing memory leak! 
       filepath = [docpath stringByAppendingPathComponent:file]; 
       //if([filepath isEqualToString:testfile]) 
       //{ 
        // deletefile = NO; 
        // break; 
       //} 
       [readPool drain]; 
      } 

      if(deletefile) 
      { 
       [self logText:[@"\nD: " stringByAppendingString:[@"docs/" stringByAppendingPathComponent:file]]]; 
       [filemanager removeItemAtPath:[docpath stringByAppendingPathComponent:file] error:NULL]; 
      } 
     } 
    } 
+2

메모리가 부족할 수 있으므로 언급 한 행이 최고 수위에 도달하는 남은 몇 바이트를 할당 할 수 있습니다. 바깥 쪽 루프에서는 메인 루프가 현재 이벤트 처리를 마칠 때까지 해제되지 않는 많은 로깅 문자열을 만듭니다. autorelease 풀은 도움이되지 않습니다. – Codo

답변

1

나는 내부 "for"루프를 PHP in_array() 함수의 Objective C와 동일하게 바꾸 었으며 메모리 문제가 사라졌습니다!

NSUInteger matchint = [allFiles indexOfObject:[docpath stringByAppendingPathComponent:file]]; 
    if(matchint != NSNotFound) 
     deletefile = NO; 
1

break 문은 NSAutoreleasePool에서 -drain을 호출하지 않고 내부 for 루프에서 빠져 나오고있었습니다.

+0

나는 그것을 게시 한 후에도 발견했지만 여전히 문제를 해결할 때 해결하지 못했습니다. – VinnyD