我收到一個錯誤,看起來非常清楚是一個釋放對象的消息,但我無法弄清楚我在哪裏管理內存錯誤。這是我創建持久性存儲協調器的代碼。它基於核心數據應用程序模板。我可能已經改變了一點,但我覺得並不多。持久性存儲區錯誤導致的內存管理錯誤
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
/* Reminder: in Simulator this is in /Users/<username>/Library/Application Support/iPhone Simulator/User/Applications/<bundleid>/Documents/Wordfare.sqlite */
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Wordfare.sqlite"]];
NSError *error;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
[persistentStoreCoordinator release];
persistentStoreCoordinator = nil;
}
return persistentStoreCoordinator;
}
的問題,當我從addPersistentStore得到一個錯誤,因爲我改變了我的模型發生,我想打開使用以前的模式創造出了商店。當然有一個答案是,「不要那樣做。」我不會,但我希望代碼變得健壯。上面的代碼運行沒有抱怨,但後來當我按下home鍵,應用程序與此錯誤崩潰:
2011-11-02 16:39:53.751 Wordfare[11137:207] -[__NSCFArray tryLock]: unrecognized selector sent to instance 0x5a122f0
2011-11-02 16:39:53.783 Wordfare[11137:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray tryLock]: unrecognized selector sent to instance 0x5a122f0'
* Call stack at first throw:
(
0 CoreFoundation 0x012ed5a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01441313 objc_exception_throw + 44
2 CoreFoundation 0x012ef0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0125e966 forwarding + 966
4 CoreFoundation 0x0125e522 _CF_forwarding_prep_0 + 50
5 CoreData 0x010d9ef0 -[_NSSQLCoreConnectionObsever _purgeCaches:] + 112
6 Foundation 0x00370669 _nsnote_callback + 145
7 CoreFoundation 0x012c59f9 __CFXNotificationPost_old + 745
8 CoreFoundation 0x0124493a _CFXNotificationPostNotification + 186
9 Foundation 0x0036620e -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
10 UIKit 0x0060aa0b -[UIApplication _handleApplicationSuspend:eventInfo:] + 554
11 UIKit 0x00614039 -[UIApplication handleEvent:withNewEvent:] + 4127
12 UIKit 0x0060babf -[UIApplication sendEvent:] + 71
13 UIKit 0x00610f2e _UIApplicationHandleEvent + 7576
14 GraphicsServices 0x03851992 PurpleEventCallback + 1550
15 CoreFoundation 0x012ce944 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52
16 CoreFoundation 0x0122ecf7 __CFRunLoopDoSource1 + 215
17 CoreFoundation 0x0122bf83 __CFRunLoopRun + 979
18 CoreFoundation 0x0122b840 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x0122b761 CFRunLoopRunInMode + 97
20 GraphicsServices 0x038501c4 GSEventRunModal + 217
21 GraphicsServices 0x03850289 GSEventRun + 115
22 UIKit 0x00614c93 UIApplicationMain + 1160
23 Wordfare 0x00002224 main + 102
24 Wordfare 0x000021b5 start + 53
)
terminate called after throwing an instance of 'NSException'
對不起,雙間距。我無法弄清格式。
看起來__NSCFArray在釋放持久性存儲協調器後發生了移動,但仍有一個指向持久性存儲的指針並正試圖調用它的tryLock。
真的很難說這可能是什麼。我分配持久存儲協調器,調用addPersistentStore,然後釋放它並將其設置爲零。我的代碼中沒有任何內容可以在該方法之外引用它。我可以想象得到對它的引用唯一的將是managedObjectModel。我試圖釋放,但也有不同的錯誤。
如果我註釋掉釋放persistentStoreCoordinator的行,並將其設置爲零,那麼它運行良好,但這是不對的。當然,persistentStoreCoordinator是一個實例變量。我分配它;如果我放棄了對它的引用,我應該發佈它。
我應該如何在出錯後清理持久存儲協調器?
附加信息:我已確認無法識別的選擇器正在發送到持久存儲協調器的內存地址。