Question

Help, my app is showing thread 1 error exc_bad_access(code=1, address=0x2000001) on the last curly brace of my PlayViewController.

Note: this happen when I click the continue button on my GuessViewController. The continue button calls the the PlayViewController. What i already did: enabled ZOMBIE close db

my GuessViewController:

#import "GuessViewController.h"
#import "PlayViewController.h"
#import "ViewController.h"

@interface GuessViewController ()

@end

@implementation GuessViewController 
@synthesize userInput = _userInput;
@synthesize gword;
@synthesize gletter;

int score = 0;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{


    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _userInput.delegate = self;
    self.scoreLabel.text = [NSString stringWithFormat:@"%d", score];

    // Do any additional setup after loading the view.
}  

-(void)touchesBegan:(NSSet*)touches withEvent: (UIEvent *) event{
    [_userInput resignFirstResponder];
}

-(BOOL)textFieldShouldReturn: (UITextField*)textField {
    if(textField){
        [textField resignFirstResponder];
    }
    return NO;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)checkAnswer:(id)sender {
    NSLog(@"%@", gword);
    NSLog(@"%@", gletter);
    NSString *temp = self.userInput.text;
    unichar temp2 = [temp characterAtIndex: 0];
    NSString *userletter= [NSString stringWithFormat:@"%C", temp2];
    NSString *message1 = @"The word is ";
    NSString *message2= [NSString stringWithFormat:@"%@", gword];
    NSString *fm = [message1 stringByAppendingString:message2];


    if([userletter isEqualToString:gletter]){
        UIAlertView *checkAlert = [[UIAlertView alloc] initWithTitle:@"Got it Right" message:fm  delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:@"Back to Main Menu", nil];
        score++;
        self.scoreLabel.text = [NSString stringWithFormat:@"%d", score];
        [checkAlert show];
    }else{
        UIAlertView *checkAlert = [[UIAlertView alloc] initWithTitle:@"Wrong" message:fm  delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:@"Back to Main Menu", nil];
        [checkAlert show];
    }

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex ==0){
        PlayViewController *playViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Play"];

        [self presentViewController:playViewController animated:YES completion: Nil];
    } else {
        ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
        [self presentViewController:viewController animated:YES completion: Nil];
    }
}
@end

My PlayViewController:

enter image description here

#import "PlayViewController.h"
#import "GuessViewController.h"
#import <sqlite3.h>
#import "Word.h"

@interface PlayViewController ()

@end

@implementation PlayViewController
@synthesize thewords;

NSString *word;
NSTimer *myTimer;
int randomIndex;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [self wordList];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.listChar.text = @" ";

    int r = (arc4random()%[self.thewords count]);
    word = [self.thewords objectAtIndex:r];
    NSLog(@"%@", word);
    randomIndex = (arc4random()%word.length);

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
   // Pass the selected object to the new view controller.
}
*/

- (IBAction)startGame:(id)sender {
        myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self     selector:@selector(listLetter:) userInfo:nil repeats:YES];


}


- (void)listLetter:(NSTimer *)timer
 {
    static int i = 0;
    unichar letter;

    if(randomIndex == i){
            letter = ' ';
    } else {
            letter = [word characterAtIndex: i];
    }
    self.listChar.text = [NSString stringWithFormat:@"%C", letter];
    if (++i == word.length) {
    [timer invalidate];
    i = 0;
    GuessViewController *guessViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Guess"];

    //passing some data
    guessViewController.word = word;
    guessViewController.letter = [NSString stringWithFormat:@"%C", [word characterAtIndex: randomIndex]];

        [self presentViewController:guessViewController animated:YES completion: Nil];


    }


}

-(NSMutableArray *) wordList {
    thewords = [[NSMutableArray alloc] initWithCapacity:26];

    @try {
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"LetterHunter.sqlite"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];

        if(!success){
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)){
            NSLog(@"An error has occured");
        }

        const char *sql = "SELECT * FROM WordList";
        sqlite3_stmt*sqlStatement;


        if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL)!=SQLITE_OK){
            NSLog(@"Problem with prepare statement1");
        } else {
            while(sqlite3_step(sqlStatement) == SQLITE_ROW){
                Word *word = [[Word alloc]init];

                word.wordfromdb = [NSString stringWithUTF8String:(char*)sqlite3_column_text(sqlStatement, 1)];

                [thewords addObject: word.wordfromdb];
            }
        }
        sqlite3_finalize(sqlStatement);
    }
    @catch (NSException *exception) {
        NSLog(@"Problem with prepare statement2");
    }
    @finally {
        sqlite3_close(db);
    }
}

@end

As suggested below, I tried doing this instead but still there's the error

while(sqlite3_step(sqlStatement) == SQLITE_ROW){
                NSString *temp = @"";

                temp = [NSString stringWithUTF8String:(char*)sqlite3_column_text(sqlStatement, 1)];

                [thewords addObject: temp;
            }
Was it helpful?

Solution

From debug stack, it's a problem about autorelease. And I think the problem maybe in

if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)){

I can't find db define. So I think it should be a instance property. Try this:

DbClass *tempDb = nil;
if(!(sqlite3_open([dbPath UTF8String], &tempDb) == SQLITE_OK)){
self.db = tempDb;

OTHER TIPS

What is wordfromdb? If it is string variable or any other datatype then add directly that into an array...

I think there is not need to create word object here as anyway your are not storing word object in array.

It looks to me an issue with word object. You are crating word object but adding its property only and not entire object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top