导致 EXC_BAD_ACCESS

Posted

技术标签:

【中文标题】导致 EXC_BAD_ACCESS【英文标题】:causes EXC_BAD_ACCESS 【发布时间】:2011-01-24 00:37:44 【问题描述】:

我有以下问题:

我正在使用默认的“基于导航的应用程序 | 使用核心数据进行存储”的构造。

我的 RootViewController 拥有一些类别。选择一个,导航控制器将推送另一个(默认)表视图控制器(ItemsViewController)。这被构造为 RootViewController(managedObjectContext_ 和 fetchedResultsController_)。只有一个变化:fetchedResultsController 使用谓词来获取所选类别的项目。

当导航控制器弹出,ItemsViewController释放dealloc中的fetchedResultsController_时发生错误。

- (void)dealloc 
    [fetchedResultsController_ release]; // is not nil and causes error
    [managedObjectContext_ release];
    self.category = nil;
    [super dealloc];

堆栈:

0  0x010fca63 in objc_msgSend ()
1  0x010ffbe0 in __FUNCTION__.13263 ()
2  0x00d2ef73 in -[_PFBatchFaultingArray dealloc] ()
3  0x00daa236 in -[NSFetchedResultsController dealloc] ()
4  0x00004762 in -[ItemsViewController dealloc] (self=0x4d11e00, _cmd=0xfd09d6)
5  0x00385f1d in -[UINavigationController setDisappearingViewController:] ()
6  0x003834f6 in -[UINavigationController _clearLastOperation] ()
7  0x00383e3f in -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] ()
8  0x00510e23 in -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] ()
9  0x00511fd2 in -[UINavigationTransitionView _cleanupTransition] ()
10 0x002fd665 in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] ()
11 0x002fd4f7 in -[UIViewAnimationState animationDidStop:finished:] ()
12 0x01e816cb in run_animation_callbacks ()
13 0x01e81589 in CA::timer_callback ()
14 0x00f7bfe3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
15 0x00f7d594 in __CFRunLoopDoTimer ()
16 0x00ed9cc9 in __CFRunLoopRun ()
17 0x00ed9240 in CFRunLoopRunSpecific ()
18 0x00ed9161 in CFRunLoopRunInMode ()
19 0x018cf268 in GSEventRunModal ()
20 0x018cf32d in GSEventRun ()
21 0x002db42e in UIApplicationMain ()
22 0x00002330 in main (argc=1, argv=0xbffff094)

类别选择:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

ItemsViewController *ivc = [[ItemsViewController alloc] initWithStyle:UITableViewStylePlain];
ivc.managedObjectContext = self.managedObjectContext;
ivc.category = [self.fetchedResultsController objectAtIndexPath:indexPath];

[self.navigationController pushViewController:ivc animated:YES];

[ivc release];

界面:

@interface ItemsViewController : UITableViewController <NSFetchedResultsControllerDelegate> 

    RankCategory *category;

@private
    NSFetchedResultsController *fetchedResultsController_;
    NSManagedObjectContext *managedObjectContext_;


@property (nonatomic, retain) RankCategory *category;

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

@end

实施:

@synthesize category;
@synthesize fetchedResultsController=fetchedResultsController_, managedObjectContext=managedObjectContext_;

- (NSFetchedResultsController *)fetchedResultsController 

if (fetchedResultsController_ != nil) 
    return fetchedResultsController_;


/*
 Set up the fetched results controller.
 */
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"itemName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category = %@",self.category];
[fetchRequest setPredicate:predicate];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
[predicate release];

NSError *error = nil;
if (![fetchedResultsController_ performFetch:&error]) 
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();


return fetchedResultsController_;
    

【问题讨论】:

显示您创建各种控制器的位置。 【参考方案1】:

在您的代码上运行静态分析器;构建和分析。我看到至少一个明显的保留/释放问题。

具体来说,predicate 是通过+predicateWithFormat: 创建的,而不是retained,并且在接近方法结束时是released,这可能导致从获取请求和获取结果下的过度释放控制器。

【讨论】:

谢谢。确实,谓词被释放了很多。是否与所有此类构造函数(+arrayWithObject、+imageNamed、...等等)一起使用?它们总是自动发布吗? 太棒了!很高兴它奏效了。差不多;这在 Objective-C 内存管理指南中有很好的记录。我建议您多读几遍——从长远来看,它会为您节省大量时间! 非常感谢!我现在来看看 Objective-C 内存管理指南。

以上是关于导致 EXC_BAD_ACCESS的主要内容,如果未能解决你的问题,请参考以下文章

__init__.py 是 python 的后裔,但不是来自 c++;导致“导入 matplotlib”错误

为啥使用 STL std::vector 作为 __block 变量会导致内存损坏?

解决thinkPHP构造函数__construct导致tp方法冲突问题

C ++宏导致:“警告:未使用的变量“LOG__METHOD__”“

从 pytorch 数据集返回索引:更改 __getitem__ 的函数导致元类冲突

是啥导致 Xcode 3.2 为 FMDB 提供 __unsafe_unretained 错误?