2012-02-19 4 views
0

「クライアント」と「車」の2つのエンティティがあるコアデータスタックがあります。どちらもtableViewControllerによって表されます。コアデータビューアでエラーが発生しました

最初のtableViewControllerがクライアントのリストを取得し、一度選択すると、2番目のクライアントはクライアントが所有する車のリストを表示します。両方がナビゲーションコントローラにプッシュされます。私が2番目のviewcontrollerから「戻る」に行くと、プログラムはfirstviewcontrollerを正常に表示し、2秒間待ってからクラッシュします。私が「ビルドしてデバッグ」したとき、コンソールにこのエラーが表示されました:

Program received signal: “EXC_BAD_ACCESS”. 

私はそれを理解していません。どこでエラーを見つけるべきですか?

EDIT:メモリの処理が不良であるかどうかを確認するために、いくつかのコードを追加しました。コメントアウトされたメソッドと、エラーが発生する前に使用されなかったメソッドをすべて削除しました。

これは、あなたが所有していないオブジェクトを解放している...これは私のClientCarsViewController実装です...

@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 
+0

通常、このエラーは、割り当てられていないもの、または割り当てが解除されているものにアクセスしようとしていることを意味します。その2分後にアプリがクラッシュした後は何をしていますか?エラーに関する追加情報はありませんか? – sch

+0

私は何もしません、私は戻って2秒ほど待ってから、それはクラッシュし、私にそのエラーを与えます。さらに詳しい情報はありません。私は、私がその時点で持っていけないオブジェクトをリリースしたと仮定することができます。私の投稿を編集して、それが役立つかどうかを確認するコードを貼り付けてください... –

+0

この種のエラーについては、NSZombieEnabled環境変数を有効にすることができます。これは、あなたが到達しようとしている解放されたオブジェクトを識別するのに役立ちます。 – aslisabanci

答えて

1

@implementation ClientListViewController 

@synthesize clientsArray; 
@synthesize coreDataModel; 

#pragma mark - 
#pragma mark View lifecycle 


- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Set the title 
    [email protected]"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 

私ClientListViewControllerです。 Objective-C Memory Management Rulesを見てください。あなたはそれを所有していないし、それを解放するべきではありません

Client *client = (Client *)[clientsArray objectAtIndex:indexPath.row]; 

:あなたはこのようなオブジェクトclientを得るたとえば

、。

+0

ああ... ok。ありがとう –

+0

あなたが提案したように私は3つのリリースのインスタンスを削除しました。 2番目のビューコントローラでは1、2番目のビューコントローラでは1です。実行時に、クライアントを選択して「戻る」ことができます。私は別のクライアントを選ぶことさえできます。しかし、2回目にクライアントをクリックするとすぐに、同じエラーが発生し、クラッシュします。別の欠陥もありますか? –

+0

最終的なエラーが見つかりました - 私はNSSetもリリースしていましたが、それを所有していませんでした!再度、感謝します。 –

関連する問題