开发者

Accessing variables in UITableView crash my app

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|UIViewAutoresizingFlexibl开发者_如何学JAVAeWidth;  
    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:

langsarray = [langs componentsSeparatedByString: @"\n"];

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:

self.langsarray = [langs componentsSeparatedByString: @"\n"];

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...

  1. What does langs contain before you split it into components? Are you sure that there's a real string there? Does langsarray contain what you're expecting immediately after the assignment?

  2. 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:

NSUInteger c = [self.langsarray count];
return c;

That way you can set a breakpoint and see the value of c.


You need to retain the langsarray in this line:

langsarray = [langs componentsSeparatedByString: @"\n"];

componentsSeparatedByString returns an NSArray which you do not own (did not alloc, or use method with copy..., new..., alloc...), so you need to retain/release it.


The questions:

EDIT, after some debug

I found was what wrong:

  • self.langsarray = [langs componentsSeparatedByString: @"\n"];

  • //[path release]; //[langs release];

    But i have a doubt if a have a memory leak.

And now all works fine,

Thanks Stephen

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜