The application is very simple, a nevigationviewcontroller that navigates to a tableview.
But when i´m accessing to langsarray, it crashes
Here is the relevant code:
MenuViewController.m
-(void) Settings{
TestViewController *testvc = [[TestViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController setNavigationBarHidden:NO animated:NO];
[self.navigationController pushViewController:testvc animated:YES];
[testvc release];
}
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *langsarray;
}
@property (nonatomic, retain) NSArray *langsarray;
@end
#import "TestViewController.h"
@implementation TestViewController
@synthesize langsarray;
- (void)loadView {
//Create the table
UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
self.view = tableView;
//read the info
NSString *path = [[NSBundle mainBundle] pathForResource:@"l" ofType:@"txt"];
NSString *langs = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
langsarray = [langs componentsSeparatedByString: @"\n"];
[path release];
[langs release];
[tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Accessing langsarray crash the app
//harcoded works fine: return 5;
return [langsarray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
// again, accessing langsarray crash the app
cell.textLabel.text = [[[langsarray objectAtIndex:row] componentsSeparatedByString:@"="] objectAtIndex:0];
cell.detailTextLabel.text = [[[langsarray objectAtIndex:row] componentsSeparatedByString:@"="] objectAtIndex:1];
return cell;
}
This is the output
Program received signal:
“EXC_BAD_ACCESS”. warning: Couldn’t
find minimal symbol for “_sigtramp” –
backtraces may be unreliable kill
Xcode 3.1.4
leopard
Your problem is on this line:
The value returned on the right hand side is autoreleased, meaning that it’s already been deallocated when you read it later on.
Try the following change:
This will make it use the property, which will automatically retain it for you. You could also consider retaining it manually.
Update to respond to comment…
What does
langscontain before you split it into components? Are you sure that there’s a real string there? Doeslangsarraycontain what you’re expecting immediately after the assignment?What is actually crashing in
numberOfRowsInSection? Is the value you’re returning causing the crash or is it the fact that you’re counting an corrupt object? (For example, returning zero for the number of sections in the table used to — and may still — cause a crash.)I’d split the code out like this:
That way you can set a breakpoint and see the value of
c.