2016-10-19 2 views
0

클라이언트 측 OSX 시스템에서 가끔 충돌하는 코드가 있지만 충돌을 재현 할 수 없습니다. 몇 가지 가정 만 있지만 시각 장애를 수정하고 싶지는 않습니다. 난 그냥 수정으로 확신 할 :ARC가 CoreFoundation 객체를 충돌시킵니다.

+ (NSArray *)loginItems:(NSError *__autoreleasing *)error { 

    LSSharedFileListRef list = [self loginItemsFileListRef]; 
    NSMutableArray *result = [NSMutableArray array]; 
    NSArray *items = [self loginItemsForFileListRef:list]; 
    CFRelease(list); 
    for (id item in items) { 
     LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item; 
     [result addObject:[self loginItemFromItemRef:itemRef]]; 
    } 
    return result; 
} 

+ (NSDictionary *)loginItemFromItemRef:(LSSharedFileListItemRef)itemRef { 
    NSMutableDictionary *itemDict = [NSMutableDictionary dictionary]; 
    // Name 
    CFStringRef nameRef = LSSharedFileListItemCopyDisplayName(itemRef); 
    if (nameRef) { 
     NSString *name = (__bridge NSString *)nameRef; 
     itemDict[RESLoginItemsNameKey] = name.stringByDeletingPathExtension; 
     CFRelease(nameRef); 
    } 
    // Path 
    CFURLRef URLRef = NULL; 
    if (LSSharedFileListItemResolve(itemRef, 0, &URLRef, NULL) == noErr) { 
     if (URLRef) { 
      NSURL *URL = (__bridge NSURL *)URLRef; 
      NSString *path = URL.path; 
      if (path) { 
       itemDict[RESLoginItemsPathKey] = path; 
      } 
      CFRelease(URLRef); 
     } 
    } 
    // Hidden 
    CFBooleanRef hiddenRef = LSSharedFileListItemCopyProperty(itemRef, 
                   kLSSharedFileListLoginItemHidden); 
    if (hiddenRef) { 
     if (hiddenRef == kCFBooleanTrue) { 
      itemDict[RESLoginItemsHiddenKey] = @YES; 
     } 
     CFRelease(hiddenRef); 
    } 

    return itemDict; 
} 

충돌 내부 loginItems : 즉 SharedFileListItemDeallocate가 충돌이 발생할을. NSArray의 항목이 자동으로 릴리스되었습니다. 나는 다음과 같이 가정한다.

SSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item; 

ARC 동작을 중단하고 루프가 끝나면 배열의 해제 콘크리트 항목으로 연결된다. 두 번째 릴리스는 함수 반환으로 호출되어 충돌로 이어집니다.

누구든지 내 아이디어를 증명하거나 반증 할 수 있습니까? 감사.

+0

마 체크하십시오. http://stackoverflow.com/questions/10590649/a-simple-code-that-worked-fine-under-gc-but-started-crashing-in-arc – Shekhu

답변

0

나는 문제가 반환 된 항목 list가 유효 인에 의존하는 경우 list의 출시도 items의 방출을 실행할 수 있기 때문에 당신이

[self loginItemsForFileListRef:list] 

에 의해 반환 된 항목을 사용하기 전에 list를 해제하는 것을 생각 반환 된 항목을 반복 할 때 충돌이 발생할 수 있습니다. 의 릴리스를 for-loop 다음에 이동하여 items을 반복 실행하십시오. 때로는 메모리 사용에 열중하려 할 ​​때 물기도합니다.