解雇模态视图控制器后自动释放崩溃

时间:2012-06-02 16:57:37

标签: automatic-ref-counting modalviewcontroller autorelease

我在使用以下代码创建的模态视图控制器中呈现一个表视图(http://stackoverflow.com/questions/44712​​89/how-to-filter-nsfetchedresultscontroller-coredata-with-uisearchdisplaycontroll)所以我可以使用Core数据,NSFetchedResults和UISearchbar。我终于在表视图/搜索/详细信息视图结束时一切正常,但我刚刚发现了一个我似乎无法弄清楚的showstopper。

当我使用:

关闭tableview时
  - (IBAction)doneButtonPressed:(id)sender {[self dismissModalViewControllerAnimated:YES];}

模态视图解散,主视图加载,然后应用程序崩溃。它已经足够远,我可以从主视图控制器调用这个nslog:

-(void) viewDidAppear:(BOOL)animated { NSLog(@"do I crash");}

我在这里遇到了错误的访问错误:

   int main(int argc, char *argv[]) 
   {
       @autoreleasepool {
         return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));}}

我认为问题可能与电弧有关。我使用的代码模板不是arc,所以我做了以下更改:

    @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
    @property (nonatomic, retain, readonly) NSFetchedResultsController *fetchedResultsController;

    @property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
    @property (nonatomic, strong, readonly) NSFetchedResultsController *fetchedResultsController;

    @property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
    @property (nonatomic, retain) NSFetchedResultsController *searchFetchedResultsController;
    @property (nonatomic, retain) UISearchDisplayController *mySearchDisplayController;

    @property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
    @property (nonatomic, strong) NSFetchedResultsController *searchFetchedResultsController;
    @property (nonatomic, strong) UISearchDisplayController *mySearchDisplayController;

此外,我从didreceivememorywarning中删除了以下内容:

   [fetchedResultsController_ release];
   [searchFetchedResultsController_ release];

并更改了以下内容 - (NSFetchedResultsController *)fetchedResultsController

    return [[fetchedResultsController_ retain] autorelease];

    return fetchedResultsController;

and in - (NSFetchedResultsController *)searchFetchedResultsController

    return [[searchFetchedResultsController_ retain] autorelease];

    return searchFetchedResultsController;

我完全难过了。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

嗯,问题解决了。我无法想象我是第一个或最后一个有这个问题的人,所以如果你遇到类似的困难......

搜索完搜索代码后(我认为它必须与之有关,因为如果我在关闭模态视图之前进行搜索,它就不会崩溃),我仍然找不到任何错误。

然而,我意识到我正在以编程方式创建searchBar和searchDisplayDelegate。一时兴起,我决定在故事板中创建searchBar和displayDelegate,然后将它们链接起来。一旦添加它们,我从loadView注释掉了:

//  UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
//   self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

此外,在storyboard中添加它们会在viewDidUnload中自动处理以下内容:

[self setSearchBar:nil];
[self setMySearchDisplayController:nil];

中提琴!没有更多的崩溃...我认为这只是mySearchDisplayController没有正确卸载。希望能帮到别人!