Frage

Edit: The original title for this thread was "Crash when creating a second managed object model", but the problem was solved and the problem turned out to be as described in the title above.

TL;DR: Do not reuse NSFetchRequests across multiple NSManagedObjectContext objects. See answer.


We are seeing a problem that seems to be related to NSEntityDescription reuse gone bad.

We have an app that uses Core Data per user, and allows users to login and logout. Each session has something we call Core Data stack, which loads a managed object model, store coordinator and the context hierarchy we use. When the user logs out, we release the stack, and the model, coordinator and contexts are released. However, when the users logs in again, we get a crash. Enabling zombies, we see the following:

2014-04-30 11:31:50.755 CPiPhoneSSLVPN[65981:907] *** -[NSEntityDescription name]: message sent to deallocated instance 0x19888170

This happens whenever we attempt to perform a fetch. I played around with the Core Data stack, and made the model static (it is loaded on demand once, and is never released) and this works as expected. Problem is, our app modifies at runtime the model entities to conform to a security policy (for example, we make certain properties transient so that they are not stored to disk). So making the model is not a viable solution.

Here is the backtrace of the crash:

* thread #1: tid = 0x3c6736, 0x02793411 CoreFoundation`___forwarding___ + 769, name = 'Main', queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
  * frame #0: 0x02793411 CoreFoundation`___forwarding___ + 769
    frame #1: 0x027930ee CoreFoundation`__forwarding_prep_0___ + 14
    frame #2: 0x0319e71e CoreData`fetchPlanAllocateInitialize + 206
    frame #3: 0x0319e2e4 CoreData`-[NSSQLCore objectsForFetchRequest:inContext:] + 644
    frame #4: 0x0319ddcf CoreData`-[NSSQLCore executeRequest:withContext:error:] + 383
    frame #5: 0x0319d7f2 CoreData`-[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 4466
    frame #6: 0x0319af56 CoreData`-[NSManagedObjectContext executeFetchRequest:error:] + 566
    frame #7: 0x031efd86 CoreData`-[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:] + 502
    frame #8: 0x0326da14 CoreData`__82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 676
    frame #9: 0x031efb81 CoreData`internalBlockToNSManagedObjectContextPerform + 17
    frame #10: 0x06c2d4d0 libdispatch.dylib`_dispatch_client_callout + 14
    frame #11: 0x06c1c439 libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 80
    frame #12: 0x06c2d4d0 libdispatch.dylib`_dispatch_client_callout + 14
    frame #13: 0x06c1b726 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 340
    frame #14: 0x0280843e CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
    frame #15: 0x027495cb CoreFoundation`__CFRunLoopRun + 1963
    frame #16: 0x027489d3 CoreFoundation`CFRunLoopRunSpecific + 467
    frame #17: 0x027487eb CoreFoundation`CFRunLoopRunInMode + 123
    frame #18: 0x062335ee GraphicsServices`GSEventRunModal + 192
    frame #19: 0x0623342b GraphicsServices`GSEventRun + 104
    frame #20: 0x048ddf9b UIKit`UIApplicationMain + 1225
    frame #21: 0x0003b0e6 CPiPhoneSSLVPN`main(argc=1, argv=0xbfffed7c) + 86 at main.m:17

Has anyone seen this kind of problem? Any tips?

I've opened bug report 16767152 for this issue.

War es hilfreich?

Lösung

The problem was we were using a cached NSFetchRequest. It seems the fetch requests caches the entity internally on first use by a context, and later if reused, it naively attempts to use its cache. This created the above error.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top