0

我在UINavigationControllerUITableViewController的子類中有一個NSFetchedResultsController。當我運行該應用程序時,前三次訪問視圖(進入該視圖,然後單擊「返回」,然後再次轉到該視圖),所有內容都可以正常運行,但在第四次(總是)時,它會崩潰並顯示以下內容:NSFetchedResultsController initWithFetchRequest拋出EXE-BAD-ACCESS異常

-[NSEntityDescription subentitiesByName]: message sent to deallocated instance 0x8b09c80

任何幫助將不勝感激。


這裏是我的結果控制器吸氣:

- (NSFetchedResultsController*)eventsResultsController { 

    if (eventsResultsController_ == nil) { 

    NSFetchRequest *aFetchRequest = [[PADataContext sharedInstance] makeGetAllFetchRequestForEntity:@"PAEvent" sortedBy:@"when" ascending:NO]; 

    // NOTE: crashes on this next line 
    NSFetchedResultsController *aFetchedResultsContorller = [[NSFetchedResultsController alloc] initWithFetchRequest:aFetchRequest managedObjectContext:[[PADataContext sharedInstance] managedObjectContext] sectionNameKeyPath:@"whenMonth" cacheName:@"AllEvent"]; 

    self.eventsResultsController = aFetchedResultsContorller; 

    [aFetchedResultsContorller release]; 

    } 

    return eventsResultsController_; 

} 

這是我用它來創建的代碼我NSFetchRequest

- (NSFetchRequest*)makeGetAllFetchRequestForEntity:(NSString*)entityName sortedBy:(NSString*)sortString ascending:(BOOL)ascending { 

    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]]; 
    [fetchRequest setEntity:entity]; 

    if (sortString != nil) { 

    // add sorting information 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortString ascending:ascending]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 

    [fetchRequest setSortDescriptors:sortDescriptors]; 

    [sortDescriptors release]; 
    [sortDescriptor release]; 

    } 

    [entity release]; 
    return fetchRequest; 

} 

上下文對象被創建一次,整個舉行單身人士的應用程序的生命週期。

我已經檢查確保當視圖控制器得到dealloc ed時,eventsResultsController_得到釋放。

在堆棧中,有人告訴我它在initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName方法墜毀:

#0 0x00f04057 in ___forwarding___ 
#1 0x00f03f22 in __forwarding_prep_0___ 
#2 0x00da1b4d in -[NSFetchedResultsController initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:] 
#3 0x00008c94 in -[PAHistoryListTableView eventsResultsController] at PAHistoryListTableView.m:125 
#4 0x00008a5b in -[PAHistoryListTableView loadData] at PAHistoryListTableView.m:52 
#5 0x00008a2a in -[PAHistoryListTableView viewDidLoad] at PAHistoryListTableView.m:43 
... 

回答

3

你與非固定電話創建您的實體:

NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]]; 

然後鬆開手:

[entity release]; 

取出發佈。當你關注的是過度發佈的NSEntityDescription這個事實時,這很容易找到。你只能在一個地方使用它,所以很容易發現錯誤。

+0

'NSFetchRequest`的`setEntity`方法不會保留它嗎? – 2011-02-04 16:10:34