2013-10-19 85 views
5

下面的代碼時,我從歷史記錄核心數據對象登錄/請求的值返回以下錯誤:[CFNumber釋放]:消息發送到釋放的實例

-[CFNumber release]: message sent to deallocated instance 0x17ea2a90 

我原先認爲存在這樣的問題別處,並具有花了無數個小時試圖調試這沒有運氣。在進一步測試之後,我已經指出崩潰從歷史核心數據對象請求某些值。任何人都可以看到任何問題,爲什麼對象值被釋放?

[[DocumentHandler sharedDocumentHandler] performWithDocument:^(UIManagedDocument *document) { 

    if(!self.document) { 
     self.document = document; 
    } 

    __block NSString *identifier = [[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultsUserIdentifier]; 

    [self.document.managedObjectContext performBlock:^{ 

     // Create history object 
     History *history  = [NSEntityDescription insertNewObjectForEntityForName:@"History" 
                   inManagedObjectContext:self.document.managedObjectContext]; 
     history.identifier = identifier; 
     history.followers  = [NSNumber numberWithInteger:53]; 
     history.following  = [NSNumber numberWithInteger:53]; 
     history.newFollowers = [NSNumber numberWithInteger:53]; 
     history.unfollowers = [NSNumber numberWithInteger:53]; 
     history.fans   = [NSNumber numberWithInteger:53]; 
     history.mutualFriends = [NSNumber numberWithInteger:53]; 
     history.nonFollowers = [NSNumber numberWithInteger:53]; 

     [self.document saveToURL:self.document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) { 

      // Fetch previous records 
      NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"History"]; 
      request.predicate 
      = [NSPredicate predicateWithFormat:@"identifier == %@", identifier]; 

      NSError *error = nil; 

      NSArray *records = [self.document.managedObjectContext executeFetchRequest:request error:&error]; 

      [records enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
       NSLog(@"%@", [(History *)obj newFollowers]); // Crash takes place here 
      }]; 

     }]; 

    }]; 

}]; 

多個日誌有異常斷點啓用:

(lldb) bt 
* thread #1: tid = 0x28b70, 0x3b5787fa libobjc.A.dylib`objc_release + 10, queue = 'com.apple.main-thread, stop reason = EXC_BAD_ACCESS (code=1, address=0x10) 
    frame #0: 0x3b5787fa libobjc.A.dylib`objc_release + 10 
    frame #1: 0x3b578022 libobjc.A.dylib`(anonymous namespace)::AutoreleasePoolPage::pop(void*) + 358 
    frame #2: 0x311b4298 CoreFoundation`_CFAutoreleasePoolPop + 16 
    frame #3: 0x3127533e CoreFoundation`__NSArrayEnumerate + 614 
    frame #4: 0x31216012 CoreFoundation`-[NSArray enumerateObjectsWithOptions:usingBlock:] + 62 
    frame #5: 0x000eae26 Twitter Followers`__71-[MainViewController updateCurrentRecordAndTableHeaderForUpdateAction:]_block_invoke_2(.block_descriptor=0x15e58080, success='\x01') + 678 at MainViewController.m:671 
    frame #6: 0x3ba65bea libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66 
    frame #7: 0x3ba600ee libdispatch.dylib`_dispatch_client_callout + 22 
    frame #8: 0x3ba629a8 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 268 
    frame #9: 0x3124b5b8 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8 
    frame #10: 0x31249e84 CoreFoundation`__CFRunLoopRun + 1308 
    frame #11: 0x311b4540 CoreFoundation`CFRunLoopRunSpecific + 524 
    frame #12: 0x311b4322 CoreFoundation`CFRunLoopRunInMode + 106 
    frame #13: 0x35eeb2ea GraphicsServices`GSEventRunModal + 138 
    frame #14: 0x33a6b1e4 UIKit`UIApplicationMain + 1136 
+0

在哪一行究竟異常發生的?你能提供一個堆棧回溯? –

+0

它發生在[記錄enumerateObjectsUsingBlock ...]期間。我剛剛添加了更多日誌。這些日誌有一個異常斷點。 – morcutt

+0

我也遇到了這個錯誤,但顯然是出於不同的原因(沒有以「new」開頭的屬性)。是否有可能使用它的地址獲得關於釋放對象的更多信息? – Symmetric

回答

21

按照Transitioning to ARC Release Notes

,使其與手動保留釋放碼,ARC強加給方法命名約束:

  • 您不能給訪問者一個以new開頭的名稱。這反過來又意味着,你不能,例如,除非您指定一個不同的吸附申報名稱以new屬性:

    // Won't work: 
        @property NSString *newTitle; 
    
        // Works: 
        @property (getter=theNewTitle) NSString *newTitle; 
    

底線,與new啓動方法是假設返回ARC負責管理和釋放的對象(但這裏不是這種情況)。我建議只是改變你的財產,以便它不以new開頭,以避免混淆。

+4

+1非常漂亮的漁獲 –

+1

哇。有用。我從來沒有抓到過。非常感謝! – morcutt

+0

Omg謝謝你的提示!我現在在覈心數據實體中擁有「新」屬性2年。將應用程序升級到ios7時,應用程序幾乎每次都會崩潰。重命名的財產和booom –

相關問題