2013-10-19 11 views
5

Il codice di seguito restituisce il seguente errore quando accedo valori/richiesta dall'oggetto Storia Core Data:[rilascio CFNumber]: messaggio inviato ad esempio deallocato

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

Ho inizialmente pensato che ci fosse un problema altrove e hanno trascorso innumerevoli ore cercando di eseguire il debug di questo a nessuna fortuna. Dopo ulteriori test, ho individuato l'arresto anomalo per la richiesta di determinati valori dall'oggetto Data Core della cronologia. Qualcuno può vedere qualche problema sul perché i valori degli oggetti sono stati deallocati?

[[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 
      }]; 

     }]; 

    }]; 

}]; 

Più tronchi con un punto di arresto eccezione abilitati:

(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

Su quale riga si verifica esattamente l'eccezione? Puoi fornire uno stack backtrace? –

+0

Si verifica durante [records enumerateObjectsUsingBlock ...]. Ho appena aggiunto altri registri. Questi registri sono con un punto di interruzione di eccezione. – morcutt

+0

Ricevo anche questo errore, ma apparentemente per un motivo diverso (non ci sono attributi che iniziano con "nuovo"). È possibile ottenere ulteriori informazioni sull'oggetto deallocato utilizzando il suo indirizzo? – Symmetric

risposta

21

Secondo la Transitioning to ARC Release Notes:

per consentire l'interoperabilità con codice manuale trattenere release, ARC impone un vincolo metodo di denominazione :

  • Non è possibile assegnare un nome utente che inizi con new. Questo a sua volta significa che non è possibile, per esempio, dichiarare una proprietà il cui nome inizia con new a meno che non si specifica un getter diversa:

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

Linea di fondo, i metodi che iniziano con new si assume restituire un oggetto che ARC è responsabile della gestione e del rilascio (ma non è questo il caso). Ti suggerirei di modificare la proprietà in modo che non inizi con new per evitare qualsiasi confusione.

+4

+1 cattura molto bella –

+1

Wow. Funziona. Non l'avrei mai preso. Grazie mille! – morcutt

+0

Omg grazie per questo suggerimento! Ho avuto una proprietà "nuova" in un'entità dati di base per 2 anni. Durante l'aggiornamento dell'app a ios7, l'app si è bloccata quasi ogni volta. Rinominato la proprietà e booom –

Problemi correlati