Objective c coredata视图控制器出错
我有一个核心数据栈,其中有两个实体:“客户机”和“汽车”。两者都由TableViewController表示 第一个tableViewController获取客户机列表,然后一旦选中,第二个将显示客户机拥有的汽车列表。两者都被推到导航控制器上。当我从第二个viewcontroller“返回”时,程序成功显示第一个viewcontroller,等待一秒钟左右,然后崩溃。当我执行“构建和调试”时,控制台出现以下错误:Objective c coredata视图控制器出错,objective-c,ios,core-data,Objective C,Ios,Core Data,我有一个核心数据栈,其中有两个实体:“客户机”和“汽车”。两者都由TableViewController表示 第一个tableViewController获取客户机列表,然后一旦选中,第二个将显示客户机拥有的汽车列表。两者都被推到导航控制器上。当我从第二个viewcontroller“返回”时,程序成功显示第一个viewcontroller,等待一秒钟左右,然后崩溃。当我执行“构建和调试”时,控制台出现以下错误: Program received signal: “EXC_BAD_ACCESS
Program received signal: “EXC_BAD_ACCESS”.
我不明白。我应该在哪里查找错误
编辑:我在下面包含了一些代码,看看这是否是由于内存处理不好造成的。。。我已经删除了所有已注释掉的方法,以及在错误出现之前未使用的方法
这是我的ClientListViewController
@implementation ClientListViewController
@synthesize clientsArray;
@synthesize coreDataModel;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Set the title
self.title=@"Clients";
[self populateTable];
}
-(void)populateTable {
[self setClientsArray:[coreDataModel retrieveClientList]];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
return YES;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [clientsArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
Client *client = (Client *)[clientsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [client name];
return cell;
[client release];
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Create and push new view controller.
ClientCarsViewController *clientCarsViewController = [[ClientCarsViewController alloc] initWithNibName:@"ClientCarsViewController" bundle:nil];
//Pass the CoreDataModel to the view controller
clientCarsViewController.coreDataModel = coreDataModel;
// Pass the selected object to the new view controller
Client *client = (Client *)[clientsArray objectAtIndex:indexPath.row];
clientCarsViewController.client = client;
// Push the new viewController
[self.navigationController pushViewController:clientCarsViewController animated:YES];
// Release the objects
[clientCarsViewController release];
[client release];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
self.clientsArray = nil;
}
- (void)dealloc {
[clientsArray release];
[coreDataModel release];
[super dealloc];
}
@end
这是我的客户端CARSVIEWCONTROLLER实现
@implementation ClientCarsViewController
@synthesize carsArray;
@synthesize coreDataModel;
@synthesize client;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.title = client.name;
// Get client's cars
NSSet *cars = client.cars;
// Import them into the carsArray
[self setCarsArray: [NSMutableArray arrayWithArray:[cars allObjects]]];
[cars release];
}
-(void)addCarToClient {
[coreDataModel addCarToClient:(Client *)client];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
return YES;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [carsArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
Car *car = (Car *)[carsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [car carName];
return cell;
[car release];
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
self.carsArray = nil;
}
- (void)dealloc {
[self.client release];
[self.coreDataModel release];
[self.carsArray release];
[super dealloc];
}
@end
您正在释放您不拥有的对象。让我们看看Objective-C 例如,当您获得一个对象
客户机时,如下所示:
Client *client = (Client *)[clientsArray objectAtIndex:indexPath.row];
您不拥有它,也不应该释放它。该错误通常意味着您试图访问未分配或已解除分配的内容。在应用程序崩溃后的那一秒,你会怎么做?你没有关于错误的任何其他信息吗:发生错误的线路,等等?我什么都不做,我点击“后退”等一秒钟左右,然后它崩溃并给我这个错误。没有进一步的信息。我只能假设到那时我已经释放了一个我不应该拥有的物体。我将编辑我的帖子并粘贴一些代码,看看这是否有帮助……对于这些类型的错误,您可以启用NSZombieEnabled环境变量。这有助于您确定要访问的已发布对象。正如您所建议的,我已经删除了3个发布实例。第一个视图控制器中有2个,第二个视图控制器中有1个。在运行时,我现在可以选择一个客户端并成功返回。我甚至可以选择另一个客户。然而,当我第二次点击一个客户端时,我得到了同样的错误,它崩溃了。还有其他故障吗?发现了最后一个错误-我也发布了NSSet,但它不是我自己的!再次感谢。