使用@synthesize时EXC_BAD_ACCESS

时间:2011-03-24 22:24:21

标签: objective-c

.h文件

#import <UIKit/UIKit.h>
#import "Tardies.h"
#import "DBAccess.h"

@interface TardiesViewController : UITableViewController
<UITableViewDelegate, UITableViewDataSource, UIPrintInteractionControllerDelegate> {
    NSMutableArray *tardiesAry;
    UITableView *table;
}

@property (nonatomic, retain) NSMutableArray *tardiesAry;
@property (nonatomic, retain) IBOutlet UITableView *table;

@end

.m文件

#import "TardiesViewController.h"


@implementation TardiesViewController

@synthesize tardiesAry;
@synthesize table;

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    DBAccess *dbAccess = [[DBAccess alloc] init];
    self.tardiesAry = [dbAccess getTardies];
    [dbAccess closeDatabase];
    [dbAccess release];
    [table reloadData];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    self.tardiesAry = nil;
}


- (void)dealloc {
    [tardiesAry release];
}

@end

- (NSMutableArray *) getTardies
{   
//  [self deleteFromTardies:@""];
NSLog (@"DBAccess - getTardies - 1");
NSMutableArray *tardies = [[[NSMutableArray alloc] init] autorelease];

const char *sql = "SELECT tardies.studentId, tardyDate, tardyTime, lastName, firstName, middleName FROM tardies LEFT JOIN students ON tardies.studentId = students.studentId ORDER BY tardyDate desc, tardyTime desc, lastName, firstName, middleName";

sqlite3_stmt *statement;

int sqlResult = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);

if ( sqlResult == SQLITE_OK)
{
    NSLog (@"DBAccess - getTardies - 6");
    while (sqlite3_step(statement) == SQLITE_ROW )
    {
        Tardies *tardy = [[Tardies alloc] init];

        char *stuId = (char *) sqlite3_column_text(statement, 0);
        char *trdyDate = (char *) sqlite3_column_text(statement, 1);
        char *trdyTime = (char *) sqlite3_column_text(statement, 2);
        char *lName = (char *) sqlite3_column_text(statement, 3);
        char *fName = (char *) sqlite3_column_text(statement, 4);
        char *mName = (char *) sqlite3_column_text(statement, 5);

        tardy.studentId = (stuId) ? [NSString stringWithUTF8String:stuId] : @" ";
        tardy.tardyDate = (trdyDate) ? [NSString stringWithUTF8String:trdyDate] : @" ";
        tardy.tardyTime = (trdyTime) ? [NSString stringWithUTF8String:trdyTime] : @" ";

        NSString *lNm = (lName) ? [NSString stringWithUTF8String:lName] : @" ";
        NSString *fNm = (fName) ? [NSString stringWithUTF8String:fName] : @" ";
        NSString *mNm = (mName) ? [NSString stringWithUTF8String:mName] : @" ";
        tardy.fullName = [NSString stringWithFormat:@"%@, %@ %@", lNm, fNm, mNm];

        NSDictionary *tardyDict = [[NSDictionary alloc] initWithObjectsAndKeys:tardy.studentId, @"studentId", 
                                   tardy.tardyDate, @"tardyDate",
                                   tardy.tardyTime, @"tardyTime",
                                   tardy.fullName, @"fullName",
                                   nil];
        [tardies addObject:tardyDict];

        [tardy release];
        [tardyDict release];
    }
    //  Finalize the statement to release its resources
    sqlite3_finalize(statement);        
}
else 
{
    NSLog (@"Problem with the database:");
    NSLog (@"%d", sqlResult);
}
return tardies;
}

- (void) closeDatabase
{
    //  Close the database
    if (sqlite3_close(database) != SQLITE_OK) 
    {
    //  NSAssert1(0, @"Error: failed to close database: '%s' .", sqlite3_errmsg(database));
    } 
}

Xcode版本4.0 Build 4A304a

setTardiesAry第一次运行时,没问题,第二次,程序在@synthesize tardiesAry行上失败并显示错误: 线程1:编程接收信号“EXC_BAD_ACCESS”。

我尝试重写方法setTardiesAry,在该方法中失败而不是@synthesize行。

请帮助,我不知道如何继续。

它在getTardies中有一个自动释放。

1 个答案:

答案 0 :(得分:0)

修改

这里有你的dealloc

- (void)dealloc {

    [self.tardiesAry release];

}